Home Web Design More Fun with htaccess

More Fun with htaccess PDF Print E-mail
Tuesday, 13 April 2010 18:43

More and more I have had to be delving into the powerful tool of htaccess. When dealing with a large, dynamic site, sometimes it can take a good bit of research to put together the right code to get things to work, and with Search Engine Optimization (SEO) figuring out how to write redirects for dynamic pages can take quite a while. Code for simple page redirects looks much like this:

RewriteEngine On
Options +FollowSymlinks
Redirect Permanent /pagename.html http://www.domainname.com/newpagename.html

The first two lines must be in place in the htaccess file, once only, for redirects to work. The 3rd and last line is the actual redirect code.This produces a 301 redirect, necessary for search engine optimization purposes to retain the page rank of the original page.

One could also write:

RewriteEngine On
Options +FollowSymlinks
Redirect 301 /pagename.html http://www.domainname.com/newpagename.html

The only difference is in typing out "permanent" vs. "301".

In this case though, the page I wanted to redirect looked like this


The normal simpler method of redirection doesn't work because of that question mark and id at the end. The working code I finally came up with looked like this:

RewriteEngine On
Options +FollowSymlinks

RewriteCond %{QUERY_STRING} id=2
RewriteRule dynamicpage.php http://www.domainname.com/newpagename.php? [R=301,L]

Note that the RewriteCond line show the id=2. This was done because there was a particular page I wanted to have redirected to a new static page. It would need to be done differently if I was doing a general rewrite of all the dynamic pages served from the same dynamicpage.php.

In the second line, note the question mark at the end of the new URL. That ? removed the dynamic part of the url, " ?id=2", which otherwise shows up on the rewritten url when the page is redirected. And of course we add the R=301 to make sure it's a permanent redirect (without the =301 the server would automatically make it a temporary redirect) and the "L" says it's the last rule.

This solved the problem, and it tests as search engine friendly.

TonerDesign.biz, Joomla templates by SG web hosting