My current .htaccess looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
RewriteRule /([^/.]+)/?$ /restaurants.php?id=$1 [L,NC,QSA]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
I want my urls to be SEO friendly, which I figured out how to rewrite example.com/restaurants.php?id=123 to www.example.com/name/123.
The issue is when you go to non-www example.com/name/123, it redirects to the ugly url example.com/restaurants.php?id=123, which then redirects back to the SEO friendly clean www.example.com/name/123
How can I fix the non-www version to work?
Thanks for your time!
2
Answers
Talking about the “www” you can see something here:
.htaccess Remove WWW from URL + Directories
To the URL, you can do something like:
In your case (you’ve used “name” in place of “restaurants.php”), simply:
Here you can get a lot of explanations and examples:
https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
Have your rules in this order:
Make sure to clear your browser cache whiel testing this.