Forum Moderators: phranque

Message Too Old, No Replies

Redirect/rewrite for parked domain

         

camille

7:39 pm on May 5, 2008 (gmt 0)

10+ Year Member



I need help with redirect/rewrite. I am trying to be clever! I have a SSL certificate for my website (call it abc.com). I want my parked domain, xyz.com to have abc.com in the address bar so the SSL certificate will be 'ok'. (If xyz.com is in the address bar, the javascript popup will say IDAuthority not available for this site.)

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]

jdMorgan

1:46 pm on May 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Camille,

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

camille

3:56 pm on May 6, 2008 (gmt 0)

10+ Year Member



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.

jdMorgan

4:18 pm on May 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, you don't want to combine them. You want to instruct the client to go to www.abc.com so you can use your cert, and then when the client makes a new request for "www.abc.com", internally "map" that request to /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]

I'm assuming that you don't have any other subdomains or domains that you wish to keep separate -- i.e. that you do not want to redirect to www.abc.com or rewrite to the subfolder. Both rules will need to change if you do.

Jim

camille

4:49 pm on May 6, 2008 (gmt 0)

10+ Year Member



Awesome, I get it now. I misunderstood originally. Thank you so much; I appreciate it.

Camille

g1smd

12:00 am on May 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is this line correct Jim? And if it is, what does it do?

RewriteCond $1 !^store/

coopster

3:01 pm on May 7, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It says if the request does not specifically begin with the store directory then execute the rule, which in turn does an internal redirect to the store directory (and appends the rest of the original request to the store directory). The caret symbol marks the beginning of the string, the exclamation mark negates the expression.

g1smd

11:24 pm on May 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I was more interested in the $1, and why it is there.

That's different to anything I have ever used before.

jdMorgan

12:14 am on May 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The $1 back-references the URL-path captured in the RewriteRule, just as it would if it were in the substitution URL of the rule itself.

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