Forum Moderators: phranque

Message Too Old, No Replies

Rewriting domain names

         

hyperbole

7:24 am on Feb 28, 2004 (gmt 0)

10+ Year Member



I have a domain my-domain.com with an alias mydomain.com. Both point to the same IP. I would like to have the two domains deliver two different files.

For example, if the user asks for http://my-domain.com/index.html the server would deliver /index-a.html and if the user asks for http://mydomain.com/index.html the server would deliver /index-b.html

I have tried the following rewrites and they give me server error


RewriteCond %{HTTP_HOST} mydomain.com [NC]
RewriteRule ^.*$ /index-b.html [L]
RewriteCond %{HTTP_HOST} my-domain.com [NC]
RewriteRule ^.*$ /index-a.html [L]

Could anyone please tell me what I'm doing wrong.

[edited by: jdMorgan at 12:24 am (utc) on Sep. 4, 2007]
[edit reason] De-linked [/edit]

jdMorgan

11:17 pm on Feb 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hyperbole,

There's nothing fatally wrong with your code, although it doesn't quite match what you said you want to do. There's nothing there that should cause a server error.

However, it may be a case of what's not there, and whether you just didn't post it, or you actually don't have it in your .htaccess file.

Here's a simple test:


Options +FollowSymLinks
RewriteEngine on
RewriteRule ^silly\.html$ /index-a.html [L]

To test this code, request the non-existent page "silly.html" and your server should return the contents of "index-a.html". Once you get that working, things will be a lot easier, and we can work on the details later.

You may have to remove the Options directive if it causes a server error, but mod_rewrite will not run without FollowSymLinks enabled, either in .htaccess or in the server configuration.

Jim

hyperbole

4:30 am on Mar 1, 2004 (gmt 0)

10+ Year Member



Jim, Thanks for your response. I tried the lines you suggested and they work just fine. If I enter wither my-domain.com/silly.html or mydomain.com/silly.html I get back index-a.html. What is the next step?

jdMorgan

5:09 am on Mar 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The next step is to fully-define what you mean by this:
I would like to have the two domains deliver two different files. For example, if the user asks for http://my-domain.com/index.html the server would deliver /index-a.html and if the user asks for http://mydomain.com/index.html the server would deliver /index-b.html


So the question is, do you want:
  1. to redirect requests for the index file to index-a or index-b -or-
  2. to redirect requests for *any* files to index-a or index-b -or-
  3. to redirect requests for <anyfile> to <anyfile>-a or <anyfile>-b -or-
  4. none of the above
all depending on the requested hostname ("domain").

And you'll need to give an example of the difference between your two hostnames (Please do so without posting real domain names, because I must delete real ones in accordance with our Terms of Service [webmasterworld.com]).

Defining the problem precisely quickly leads to precise solutions.

Jim

hyperbole

5:21 am on Mar 1, 2004 (gmt 0)

10+ Year Member




So the question is, do you want:

1. to redirect requests for the index file to index-a or index-b -or-
2. to redirect requests for *any* files to index-a or index-b -or-
3. to redirect requests for <anyfile> to <anyfile>-a or <anyfile>-b -or-
4. none of the above

all depending on the requested hostname ("domain").

I want to direct requests for my-domain.com/<anyfile> to <anyfile>-a and requests for mydomain.com/<anyfile> to index-b.html

I think that is closest to #4.

jdMorgan

5:36 am on Mar 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then that would be something like:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?my-domain\.com
RewriteRule ^([^.]*)\.html$ /$1-a.html [L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com
RewriteRule \.html$ /index-b.html [L]

This can be further simplified if you only have those two domain names. If so, the second RewriteCond is not needed, since if it's not the first domain, then it has to be the second.

You could also redirect the two domains to two different subdirectories in order to keep things organized by domain name.

These are both server-internal rewrites, and as such, the URL shown in the visitor's address bar remains unchanged.

Jim

hyperbole

6:19 am on Mar 1, 2004 (gmt 0)

10+ Year Member



Jim,

This is the htaccess file I'm using:


Options +FollowSymLinks
RewriteEngine on
RedirectMatch (.*)other\.html$ /analysis.html [L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com
RewriteRule \.html$ /index-b.html [L]

I realized that what I really want to do is the following
my-domain.com/<anyfile> --> <anyfile>
mydomain.com/<anyfile> --> index-b.html

The above seems to work for my-domain.com but I get a 500 server error if when I ask the browser to get any file from mydomain.com. Do you have any suggestions?

jdMorgan

7:03 am on Mar 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Regarding the 500-Error: RedirectMatch is not part of mod_rewrite, and so does not support flags such as [L].
I do not know what you wish to accomplish with your RedirectMatch, and so cannot address it further.

I realized that what I really want to do is the following
my-domain.com/<anyfile> --> <anyfile>
mydomain.com/<anyfile> --> index-b.html

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com
RewriteRule \.html$ /index-b.html [L]

Note that the above is technically rewriting <anyfile>.html --> index-b.html, because you should not try to redirect from one filetype to another, with the exception of redirecting to a script that will output the correct MIME-type content for the requested filetype. In simpler terms, trying to redirect from picture.gif to mypage.html is not going to work, so the above only rewrites html file requests to index-b.html

If you want to avoid duplicate-content issues, then you might want to use an external permanent redirect, which would look like this:


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com
RewriteRule \.html$ http://%1mydomain.com/index-b.html [R=301,L]

As I said, precisely defining the problem to be solved with mod_rewrite is the difficult part. Almost everything else is available from the document here [httpd.apache.org], and from the links in that document.

A general suggestion is to never add more code until what you have works perfectly. For example, I have no way to tell if fixing the RewriteMatch syntax error will solve your problem, because you might actually have two problems. So, go one step at a time, and test, test, test. Another general suggestion is that if you need a solution fast, take your time and work slowly and carefully... Because a quickly-completed solution that doesn't work properly is always worse that a slightly-late solution that works perfectly. I've got plenty of my own 500-Server Errors to back up my opinion, too! ;)

Jim

hyperbole

10:11 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



Jim,

Thanks for your help with my Rewrite problem. I was able to finally get it to work by using the following rewrite rules:


RewriteCond %{HTTP_HOST} (www\.)?mydomain\.com
RewriteRule \.html$ /index-b.html [R=permanent]

This, of course, causes a redirect so the URL displayed in the users browser is http://www.my-domain.com/index-b.html. I had hoped to do this with a simple rewrite so the users browsers would still display http://www.mydomain/<anyfile.html>.

I had several items of confusion that you helped me to clear up. 1) I had not realized that the Pattern in the RewriteRule is only matching the URI. I thought that this was creating a match and replace kind of operation. From your last e-mail I realized that anything I place in the Substitution string will replace the entire URI if the pattern matches the current URI.

2) I had not thought about the issues involved with switching the file type (and therefore the MIME type) in a Rewrite. That is useful and interesting information.

3) I had been under the impression that placing the [L] command at the end of a RewriteRule would cause the server to stop on that line not continue to process. This is what was causing my biggest problem because the server would rewrite mydomain.com/<anyfile.html> to mydomain.com/index-b.html and then re process this URI. Since the new URI contained mydomain.com, instead of my-domain.com, the RewriteCond would match it again and the server would go into a loop. This is what was causing the 500 errors.

Thanks again for your help. I will continue to experiment with this and learn nore about Rewriting.

Carl.

[edited by: jdMorgan at 12:25 am (utc) on Sep. 4, 2007]
[edit reason] De-linked. [/edit]

jdMorgan

10:24 pm on Mar 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Since the new URI contained mydomain.com, instead of my-domain.com, the RewriteCond would match it again and the server would go into a loop.

Obviously, these are not your real domain names, and we're not allowed to post real ones anyway, so I can't offer specifics. But the usual solution is to set up the pattern in the RewriteCond -- or even add another RewriteCond -- that excludes the already-rewritten URL.

The [L] flag does stop processing for the current request, but the server may still do an internal subrequest if a lower-level .htaccess file exists, or if a check for file-exists is required. If that happens, all .htaccess files above that level are re-run.

You should be able to fix this problem without using an external redirect ([R=301] and [R=permanent] are entirely equivalent). So look into your hostnames and URLs and see if you can't adjust the patterns so that a previously-rewritten URL is excluded and therefore left alone, so it won't cause a loop.

Jim

hyperbole

12:58 am on Mar 3, 2004 (gmt 0)

10+ Year Member



Thanks Jim,

Adding
RewriteCond %{REQUEST_URI}!/index-b.html$

to .htaccess stopped the loop and lets the rewrite work properly.

Thanks again for your help. I've learned a lot from you.