Jay
Jay

Reputation: 312

PHP Mod Rewrite

I have a .htaccess which does a basic rewrite which looks like the following:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?url=$1 [L,QSA]

The next condition is to remove www. from the URL and looks like this:

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

The problem is, when I call a link such as:

http://somesite.com/category/subcategory/?id=123

and add the www. manually, it rewrites the URL to this:

http://www.somesite.com?url=category/subcategory/?123

The page stills load but, I've been told that's terrible for SEO. Any thoughts of how to fix this?

Upvotes: 2

Views: 81

Answers (1)

Prix
Prix

Reputation: 19528

Your current issue is that you have your WWW redirect after your main SEO rules, this is what happens behind the scenes:

  1. You access http://somesite.com/category/subcategory/?id=123
  2. Your rules internally redirect it to index.php?url=category/subcategory/?id=123
  3. Your last rule to redirect without the www will also take place and will ended up redirecting it to:

    http://somesite.com/?url=category/subcategory/?id=123
    

In order to fix that you would need your rules as follow:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?url=$1 [L,QSA]

Which leads us to your second issue, you've been using 301 redirects so your browser have cached some redirects and you will need to use a different browser temporarily to test your changes, preferable one you haven't used yet to access that site, while you clear the cache of that browser and wait for it to completely clear out.

Once the cache of your default browser has cleared you can use it as usual and you should get the same response.

Upvotes: 3

Related Questions