0

I am trying to create seo-friendly URLs from my dynamic ones with a .htaccess rewrite. I've tried a ton of different rewrites and none of them are updating the URL. My other URL rewrite condition works fine...removing ".php" from the end of the URL.

Example:

Trying to change this:

food-truck.php?city=miami-fl&name=john doe

To this:

food-truck/city/miami-fl/name/john%20doe

Here is one of the rewrites I have tried with no success:

Options +FollowSymLinks
RewriteEngine on
RewriteRule food-truck/city/(.*)/name/(.*)/ food-truck.php?city=$1&name=$2

Any suggestions appreciated. Haven't been able to figure out a solution from similar questions in Stack Overflow posts or forums.

Thank you.

EDIT: Here is what did the trick...

RewriteCond %{QUERY_STRING} ^city=([^&\s]+)&name=([^&\s]+)$
RewriteRule ^(?:food\.php|)$ /%1?/%2? [R=301,L]

RewriteCond %{QUERY_STRING} ^city=([^&\s]+)&name=([^&\s]+)$
RewriteRule ^(?:food\.php|)$ /%1/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\s\/]+)/?$ food.php?city=$1&rname=$2&r [L,QSA]
5
  • No error, the url just doesn't rewrite to anything. It just stays the same. Here is the url: www.example.com/food-truck.php?city=miami-fl&name=john doe Commented Mar 21, 2017 at 23:58
  • Apache Version 2.2.31 Commented Mar 22, 2017 at 20:28
  • So you solved your problem? Commented Mar 22, 2017 at 20:33
  • Not completely...just made progress. Once I figure it out 100% and get it writing correctly, I will provide an answer to my question. I would still like to hear any input from people on why my original RewriteRule won't work. Commented Mar 22, 2017 at 21:03
  • Your original rule is fine but it is only rewriting pretty URL to original URL. It is not redirecting old URL to pretty URL Commented Mar 22, 2017 at 21:06

1 Answer 1

1

There is an extra slash in the end of your rule. Try to remove it:

RewriteRule food-truck/city/(.*)/name/(.*) food-truck.php?city=$1&name=$2

Also maybe you need to add the beginning and end:

RewriteRule ^food-truck/city/(.*)/name/(.*)$ food-truck.php?city=$1&name=$2
Sign up to request clarification or add additional context in comments.

7 Comments

Removed it - no luck
I improved it. check now
Still not doing the trick. Can't seem to figure out what is causing the issue.
which url are you using to visit this page? is food-truck under domain root folder? Turn on rewrite log, you will see
Yea it's under the domain root folder. The url is this: www.example.com/food-truck.php?city=miami-fl&name=john doe
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.