Forum Moderators: phranque
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^xyz.com [NC]
RewriteRule ^(.*)$ http://www.abc.com/$1 [L,R=301]
I believe this would take care of the situation if someone enters xyz.com in the address bar. Is there a way to write this for www.xyz.com?
Also, if I want www.abc.com to redirect to www.abc.com/store - can I use the above code but add /store? Or would I need to do the following type of redirect:
RewriteCond %{HTTP_HOST} ^(www\.)?abc.com$
RewriteCond %{REQUEST_URI} !store/
RewriteRule ^(.*)$ store/$1
Or is this just wrong!?
[edited by: jdMorgan at 1:47 pm (utc) on May 6, 2008]
[edit reason] de-linked [/edit]
Welcome to WebmasterWorld!
First, there are two functions accomplished by the rules above: An external redirect and an internal rewrite. These are different things, and should not be confused.
An external redirect tells the client (browser or search engine robot), "The resource you asked for has moved. Ask for it again using this new address." This is a message sent back to the client instead of the content that it requested. The client will then issue a new HTTP request using the address supplied in the server's redirect response.
An internal rewrite simply maps a requested URL to a different, non-default server filepath. It says, "If we get a request for this URL-path, go get the content from that filepath, instead of using the filepath that would normally be used to server content for this URL-path.
In simple terms, you may view an external redirect as a URL-to-URL translation, and an internal rewrite as a URL-to-filename translation.
For the sake of clarity, please review your post and try to make it consistent as regards the use of the "www" subdomain. You should state whether you wish to use "www" on each domain. With that made clear, the solution is not very difficult.
Thanks,
Jim
Thanks for your reply and information! Based on what you said, it seems like a rewrite is more appropriate. A redirect seems best for a page that has moved? Or perhaps it doesnt matter, several ways to skin a cat?
I would like to use www for both domains since my ssl cert requires the www - might as well be consistent.
Im still a little fuzzy if I can combine my solution in to one rewrite --> www.xyz.com ---> www.abc.com/store and www.abc.com ---> www.abc.com/store.
The first step is an external redirect, the second is an internal rewrite:
Options +FollowSymLinks
RewriteEngine on
#
# Canonicalize the domain name: Redirect if the requested
# hostname is non-blank and is NOT precisely "www.example.com"
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Internally rewrite root directory requests to /store
RewriteCond $1 !^store/
RewriteRule (.*) store/$1 [L]
Jim
Remember that RewriteConds are not processed unless the RewriteRule pattern matches (see the mod_rewrite documentation). Therefore, RewriteConds can back-reference RewriteRule pattern-matches using $n, and RewriteRules can back-reference RewriteCond pattern-matches using %n.
Also, this is why it is good to use very-specific patterns in your rules: To avoid processing RewriteConds (and especially slow/inefficient file- or directory-exists checks and reverse-DNS lookups) unnecessarily.
Jim