Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

mod rewrite help please

one url to another

12:48 am on Jan 7, 2009 (gmt 0)

New User

5+ Year Member

joined:Oct 3, 2008
votes: 0

Hi I'd like to create a rule to do the following

Redirect any requests from www.domain1.com/* to www.domain2.com/*
[and] domain1.com/* to domain2.com/*

But to NOT redirect any subdomains ie sub.domain1.com

Thank you

[edited by: jdMorgan at 2:05 am (utc) on Jan. 7, 2009]
[edit reason] de-linked [/edit]

10:12 am on Jan 7, 2009 (gmt 0)

New User

5+ Year Member

joined:Jan 4, 2009
posts: 18
votes: 0

How can I do a 301 redirect?

at domain.com/.htaccess

Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^whatever/[^.]+)$ [domain.com...] [R=301,L]
RewriteRule ^index.htm$ [domain.com...] [R=301,L]

The second example only changes one URL.

[^.]+) and $1 work the same way here as in mod_rewrite, so you can easily change a lot of URLs with one line. The only change with redirects and mod_rewrite is the R=301 (Redirect 301).


[edited by: jdMorgan at 1:28 pm (utc) on Jan. 7, 2009]
[edit reason] No ULRs, please. See Terms of Service and Charter. [/edit]

2:00 pm on Jan 7, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
votes: 0

If domain1 and domain2 both resolve to the same virtual server "account", some corrections and improvements are needed -- a RewriteCond to test the requested hostname to assure that only domain1.com and and its www subdomain are redirected, and a bit of tweaking to the initialization and the rule:

At domain1.com/.htaccess :

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com
RewriteRule (.*) http://www.domain2.com/$1 [R=301,L]

This code intentionally does not implement one of your stated requirements: Rather, it redirects *both* domain1.com and www.domain1.com to www.domain2.com. This is to eliminate the duplicate-content problem which might otherwise result from having the same content appear at both domain2.com and www.domain2.com.

While it is true that code could (and should) be installed at domain2.com to take care of this duplication problem, the result of doing only that in this situation, would be two 'stacked' or 'chained' 301 redirects, and search engines are not so likely to pass PageRank/Link-popularity through two consecutive redirects as through one.

That is, if domain1 is redirected to domain2 and www.domain1 is redirected to www.domain2, but domain2 is not redirected to www.domain2.com, then duplicate content still appears at domain2 and www.domain2. If a redirect is used at domain2 to fix this, then a request for domain1 would result in a redirect to domain2, and that would result in a redirect to www.domain2 -- Two redirects in a row.

Best practices dictate that any given unique content should be accessible at one and only one URL on the Web; Requests for any and all variations on that URL should be redirected to that single canonical URL.

This would include the following, at least:

example.com./ (FQDN - Fully Qualified Domain Name)
example.com:80/ (appended port number)
example.com.:80/ (both)
example.com/index.html (redirect this to example.com/)
example.com/page.html?bogus-query-string or example.com/?bogus-query-string
example.com/&malformed-query-string (missing the "?" query demarcation token)
eXaMpLe.cOm (mis-cased)
example.com/url-path?name1=value1&name2=value2 versus example.com/url-path?name2=value2&name1=value1 (parameter order)
http vs. https protocol

In all cases, the request for the non-canonical URL should be 301-redirected to the canonical URL or should result in a 404-Not Found server response.


[edit] Fixed typo in RewriteCond: {} must be () [/edit]

[edited by: jdMorgan at 8:24 pm (utc) on Jan. 7, 2009]

11:36 pm on Jan 7, 2009 (gmt 0)

New User

5+ Year Member

joined:Oct 3, 2008
votes: 0

Thanks for the reply, I've made the change and it works great!