Checklist to Implement URL Rewriting
- CHECK THAT IT’S SUPPORTED
Not all Web servers support URL rewriting. If you put up your .htaccess file on one that doesn’t, it will be ignored or will throw up a “500 Internal Server Error.”
- PLAN YOUR APPROACH
First thing first, you have to figure out the mapping structure, so that the correct information is getting found everytime user browses your site.
- CREATE YOUR REWRITE RULES
All the rules have to be defined in an .htaccess file. To test whether the URL rewriting is working, you can use it in “test” subdirectory and use the [R] flag, so that you can see where things go:
RewriteEngine On RewriteRule ^.+/p/([0-9]+) product.php?id=$1 [NC,L,R] RewriteRule ^.+/c/([0-9]+) category.php?id=$1 [NC,L,R]
Now, if you visit www.myweb.com/test/product/p/123, you should be sent to www.myweb.com/test/product.php?id=123. Once you’re satisfied, move the .htaccess file to your document root and remove the [R] flag.
- CHECK YOUR PAGES
In that case, you would either need to rewrite the image locations or make the references absolute.
- CHANGE YOUR URLS
Find all the references of old URLs, and replace them with the new URLs, using a function such as GenerateUrl to consistently create the new URLs. This is the only step that might require looking deep into the underlying code of your website.
- AUTOMATICALLY REDIRECT YOUR OLD URLS
Now that the URL rewriting is in place, you probably want Google to forget about your old URLs and start using the new ones. That is when a search result brings up product.php?id=20, you’d want the user to be visibly redirected to product/p/123, which would then be internally redirected back to product.php?id=20.This is the reverse of what your URL rewriting already does. In fact, you could add another rule to .htaccess to achieve this, but if you get the rules in the wrong order, then the browser would go into a redirect loop.
Another approach is to do the first redirect in PHP, using something like the CheckUrl function above. This has the added advantage that if you rename the product, the old URL will immediately become invalid and redirect to the newest one.
- UPDATE AND RESUBMIT YOUR SITEMAP
Make sure to carry through your new URLs to your sitemap, your product feeds and everywhere else they appear.