Using mod_rewrite to fix SEO and URL re-mapping problems

remapping an old-style Querystring URL to a SEO-friendly scheme like so;

to something like this;

so the first thing, is there is no reason, in the general case to expect that your requests will come in like 1)

they could be re-ordered like so;

and if you have been using glassfish, or some Java app server that uses jsession ids for tracking non-cookie clients, you might end up with requests like so;;jsessionid=reogjorejgorejgire?var1=val1&var2=val2&var3=val3#someurlfragment

Use Case 1 - You migrated to a SEO friendly URL scheme like /index/name1/value1/name1/value2 and now all the search engine links are broken.

1) acknowledge that you are a twat. 2) fix the problem 3) you are still a twat

So you migrated site from some querystring based URL scheme like so

To something like this;

Now your logs are full of 404 messages, because all your search engine traffic is going to the former URLs.

because google indexed all the former links, and now you look like an idiot because all your traffic is getting 404 errors. Yes, you are also tit.

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/category RewriteCond %{QUERY_STRING} ^([0-9]*)$ RewriteRule ^/category(.*)$ /category/Id/%1$1

1) turn on the rewrite engine

3) only apply this rule to your scheme matches 4) collect the required information from the QUERYSTRING component 5) collect the required information from the REQUEST_URI componet 6) construct the redirect

This is effectively an internal redirect or an "internal proxy". i.e. this is transparent to the user.

Variation 1 - you want to tell the search engines, and any users with bookmarked links that you have changed the URL scheme.

You do this by sending the user an external redirect instead of the internal rule used above. The procedure is very similar but requires a rewrite option like so;

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/category RewriteCond %{QUERY_STRING} ^([0-9]*)$ RewriteRule ^/category(.*)$ /category/Id/%1$1 [R=302,L]

Why the "L" option. Basically as this is an external rewrite, you want the user and his/her browser to go away and do the redirect, and come back when that is ready. Hence you are not interested if they match any more rules until they come back with the /nam1/val1/nam2/val2 request.

Variation 2 - you want to pick out specific URL components, and discard for the rest. (they conflict with you new software, or some other stupid dumbass reason that only an idiot like you could fathom)

As you can't predict the variable ordering, (well maybe if you originally generated them you can, but this is a catch all for fuckupery)

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/category RewriteCond %{QUERY_STRING} ^([0-9]*)$ RewriteRule ^/category(.*)$ /category/Id/%1$1

Use Case 2 - google indexed your site, because the robot does not understand cookies,

Don't automatically start sessions. User POST where appropriate and GET for navigation.