homepage Welcome to WebmasterWorld Guest from 174.129.103.100
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
How?
guillermo5000




msg:1588491
 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.

 

jdMorgan




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

guillermo5000,

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]

Jim

guillermo5000




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

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

jdMorgan




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

guillermo5000,

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.

Jim

guillermo5000




msg:1588495
 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.

jdMorgan




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

guillermo5000,

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.

Jim

guillermo5000




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

Thank you, that clears it up.

ThatAdamGuy




msg:1588498
 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?

jdMorgan




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

ThatAdamGuy,

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]

Jim

ThatAdamGuy




msg:1588500
 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 :(

ThatAdamGuy




msg:1588501
 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