0

I'm using locales to drive several sites/domains from one build. I have set up a bunch of 301 redirects in the .htaccess to capture and redirect previous URLs. I've used RewriteCond to target the rules for the individual sites but these aren't working as I'd expect.

RewriteCond %{HTTP_HOST} ^site1\.com [NC]
RewriteRule ^news.asp /news/news-listings? [R=301,L] 

RewriteCond %{HTTP_HOST} ^site2\.com [NC]
RewriteRule ^news.asp /news-galleries/news-listings? [R=301,L] 

If I enter site2.com/news.asp I am redirected to the Rule stated for site1 so the user is redirected to site2.com/news/news-listings which (obviously) returns a 404 error message. Any thoughts on how should be doing the above properly?

  • Are the domains you're trying to access available exactly as written in your RewriteCond rule, i.e. site1.com not site1.com. More simply, what is the value of %{HTTP_HOST} when you request one of those domains? Try appending it to a new RewriteRule e.g. RewriteRule ^news.asp /news/news-listings?%{HTTP_HOST} [R=301,L] and then requesting a site1.com/news.asp. It should redirect to site1.com/news/news-listings?site1.com – simonhamp Dec 22 '15 at 16:21
  • It could also be as simple as making sure your RewriteCond arguments are wrapped in quotes, i.e. RewriteCond "%{HTTP_HOST}" "^site1\.com" [NC] – simonhamp Dec 22 '15 at 16:28
  • Thanks Simon - tested. If I enter site2.com/news.asp and append the HTTP_HOST I get redirected to site2.com/news/news-listings?site2.com - the redirection specified for site 1. Curioser and curioser … – Cole Henley Dec 22 '15 at 16:51
  • hmm, run it through htaccess.mwl.be and it says condition is not being met for domain. most confused – Cole Henley Dec 22 '15 at 16:58
  • Hmmm, wondering if it something to do with specifying multiple RewriteRules for each RewriteCond. Do we need to declared the cond for each URL we are rewriting? surely not! – Cole Henley Dec 22 '15 at 17:00
1

As per your comments, I've gleaned that the problem is really because of an incorrect assumption about how RewriteConds apply to RewriteRules.

So I'm guessing your .htaccess looks more like this:

RewriteCond %{HTTP_HOST} ^site1\.com [NC]
RewriteRule ^someurl.asp /folder/page? [R=301,L]
RewriteRule ^news.asp /news/news-listings? [R=301,L] 

RewriteCond %{HTTP_HOST} ^site2\.com [NC]
RewriteRule ^someurl.asp /folder/page? [R=301,L]
RewriteRule ^news.asp /news-galleries/news-listings? [R=301,L] 

So there are multiple RewriteRules to some of the RewriteCond statements. You're expecting site2.com/news.asp to go to site2.com/news-galleries/news-listings?, but instead it's going to site2.com/news/news-listings? because it's strictly obeying the mod_rewrite rules.

This is because RewriteCond statements only apply to the RewriteRule that immediately follows them. So your site2.com/news.asp request is actually matching the second RewriteRule in the above example.

RewriteMap to the rescue

If you have a lot of URLs you're needing to manually rewrite under multiple condition scenarios, this can get complicated because you would have to define the same RewriteCond statements for each RewriteRule.

Here's a better way - use RewriteMap (not yet tested):

RewriteMap "site1.com" "txt:/path/to/file/site1.com.txt"
RewriteMap "site2.com" "txt:/path/to/file/site2.com.txt"

RewriteCond %{HTTP_HOST} ^site1\.com [NC]
RewriteRule ^(.*)$ "${site1.com:$1}" [R=301,L]

RewriteCond %{HTTP_HOST} ^site2\.com [NC]
RewriteRule ^(.*)$ "${site2.com:$1}" [R=301,L]

That would be it. One RewriteMap, RewriteCond and RewriteRule per domain. Then your site2.com domain rewrite-map file would look like this, for example (simple space-seaparted, key-value pairs):

news.asp /news-galleries/news-listings?

Hope that helps provide a more flexible, manageable solution :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.