homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

htaccess to redirect domain.com to www.domain.com

 7:22 am on May 31, 2003 (gmt 0)
I am using this code to redirect visiters to domain.com to www.domain.com, and combine my pagerank into one page.

RewriteCond %{HTTP_HOST}!^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent]

However, when I use the "Check Server Page Tool" about half way down this page http://www.bruceclay.com/web_rank.htm, It says I have the following error

Error 301 - http://www.domain.com redirects to: http://www.domain.com/

Question: Is my code bad? Is there another way to do this redirect? Thanks for any advice.



 3:01 pm on May 31, 2003 (gmt 0)


No, that is exactly what you expect. The problem is the use of the word "error" in the checker software. Simply put, 3xx codes are redirect/modified status codes, 4xx codes are error codes.

However, I suggest you add the [L] flag to your RewriteRule to improve efficiency.

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



 4:23 pm on May 31, 2003 (gmt 0)

Thank you. Do I want [L] here? or just on my last rewrite rule?


 9:02 pm on May 31, 2003 (gmt 0)


You want [L] on any rule that, once applied, does not need any further rewriting. The [L] is only applied if the RewriteRule and all RewriteConds match. It means, "If you did this rewrite, then no further rewriting is required for this request." In most rewrite cases, an [L] should be used, and almost always with external 301/Moved Permanently or 302/Moved Temporarily redirects.



 9:38 pm on May 31, 2003 (gmt 0)

Great explanation! I've read Apache’s pages on this but they do not explain the [L] very well.

After applying this logic to the RewriteRules in my htaccess, it appears that they all need the [L].

What would be an example of a RewriteRule that does NOT need the [L]?

Thanks again.


 10:40 pm on May 31, 2003 (gmt 0)


Here's a trivial example:

# Rename various public relations pages and relocate to subdirectory
RewriteRule ^contact\.html$ /info1.html [NC]
RewriteRule ^about\.html$ /info2.html [NC]
RewriteRule ^corporate\.html$ /info3.html [NC]
RewriteRule ^info(.)\.html$ http://www.example.com/public/info$1.html [R=301,L]

The first three rules change the page names, and then the final one changes the subdirectory for all of them and redirects the visitor's browser - it's more efficient than two external redirects for each page.



 5:08 am on Jun 1, 2003 (gmt 0)

Thank you, that clears it up.


 11:12 pm on Jun 25, 2003 (gmt 0)

This works very well, except for one problem.

Now subdomain.mysite.com goes to www.mysite.com/subdomain/ :-(

I'm using rewrites like the following in my .htaccess:

RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} blog.mysite.com
RewriteCond %{REQUEST_URI}!blog/
RewriteRule ^(.*)$ blog/$1 [L]

How might I handle this situation differently so my subdomains are still supported, yet mysite.com/somepage.htm gets redirected to www.mysite.com/somepage.htm?


 6:56 am on Jun 26, 2003 (gmt 0)


OK, here's a stricter version. It will redirect only mysite.com to www.mysite.com.

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

Ref: Introduction to mod_rewrite [webmasterworld.com]



 7:08 am on Jun 26, 2003 (gmt 0)

Alas, while I appreciate the suggestion, that didn't do the trick either. Attempting to go to blog.mysite.com then resulted, after a long wait, in a server error :(


 10:30 am on Jun 29, 2003 (gmt 0)

I'd like to apologize! Due to the freak coincidence of my Web host provider doing a service "upgrade" :cough cough:, the timing of my .htaccess update per your suggestion was unfortunately synchronized with the down time of my server.

I've since re-tried your code suggestion, and it works perfectly. Thank you!

Now I just have to worry about all those links to me that are in the form of mysite.com/directory not counting towards my Page Rank. But that's fodder for a google forum :D

Thanks again for your help :)

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved