Forum Moderators: phranque
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]
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]
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
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
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
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 aboveall 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.
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]
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
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?
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]
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]
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
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]
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