Code Snippets

There are a few things often asked of me, regarding some .htaccess stuff. The fist is how to make your domain automatically revert to http://www.yourdomain.com if someone types in http://yourdomain.com. The reason why people are interested in this is because google sees that as 2 different sites. By reverting it, you tell google that your site is only with the www (you can also do it without), which will help with consistency of linking. If someone links to your page not using the www and you aren’t using this, it could hurt you. However if you have this in an .htaccess file uploaded to your root directory, it will revert to www.yourdomain.com no matter how they write out the link to your site. So, in the .htaccess file, here is what you want to put:

[quickcode:noclick]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com [nc]
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
[/quickcode]

This is an apache re-write. You are basically re-writing any domain without the www to become the domain with the www. It is set up as a 301 link, or permanently moved. So when search engine bots come through, they will read the change, and always redirect to the domain you want.

Search engines also will treat yourdomain.com and yourdomain.com/index.php as two different pages. To change this and revert any /index.php requests back to the home domain, you can do this in the same .htaccess file:

[quickcode:noclick]redirectMatch 301 ^/index\.php(.+)$ http://www.yourdomain.com/$1[/quickcode]

Make sure to be uniform here. It would be counter productive to have /index.php revert to http://yourdomain.com if above you made it re-write to www.yourdomain.com.

A Tip: Make sure all URLs for links in your site are absolute, not relative. This will ensure clean spidering, and also help you rank individual pages of your website, which is your goal.

This entry was posted in Design Strategies. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>