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

<p>So I'm guessing your .htaccess looks more like this:</p>

<pre><code>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] 
</code></pre>

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

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

<h3>RewriteMap to the rescue</h3>

<p>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.</p>

<p>Here's a better way - use <a href="https://httpd.apache.org/docs/trunk/rewrite/rewritemap.html" rel="nofollow"><code>RewriteMap</code></a> (not yet tested):</p>

<pre><code>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]
</code></pre>

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

<pre><code>news.asp /news-galleries/news-listings?
</code></pre>

<p>Hope that helps provide a more flexible, manageable solution :)</p>

CC BY-SA 3.0
2015-12-22T17:38:44.160Z
2015-12-22T17:38:44.160Z
simonhamp
<p>I'm using locales to drive several sites/domains from one build.
I have set up a bunch of 301 redirects in the <code>.htaccess</code> to capture and redirect previous URLs. I've used <code>RewriteCond</code> to target the rules for the individual sites but these aren't working as I'd expect.</p>

<pre><code>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] 
</code></pre>

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

CC BY-SA 3.0
2015-12-22T16:21:10.493Z
Are the domains you're trying to access available exactly as written in your RewriteCond rule, i.e. http://site1.com/ not http://www.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 http://site1.com/news.asp. It should redirect to http://site1.com/news/news-listings?site1.com
simonhamp
CC BY-SA 3.0
2015-12-22T16:28:11.643Z
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
CC BY-SA 3.0
2015-12-22T16:51:20.610Z
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
CC BY-SA 3.0
2015-12-22T16:58:52.063Z
hmm, run it through http://htaccess.mwl.be/ and it says condition is not being met for domain. most confused
Cole Henley
CC BY-SA 3.0
2015-12-22T17:00:38.750Z
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
CC BY-SA 3.0
2015-12-22T17:01:49.847Z
Ah yes you do. Multiple RewriteConds only work with a single RewriteRule. There is a 'workaround' here: http://stackoverflow.com/a/11559655/123696
simonhamp
CC BY-SA 3.0
2015-12-22T17:10:48.210Z
Actually, not so sure you can achieve what you need with that. The other alternative is to create a RewriteMap for each domain (see http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewritemap). This will reduce your cond/rule statements down considerably, but could be a little slower per request
simonhamp
CC BY-SA 3.0
2015-12-22T17:27:28.187Z
Thanks Simon - yeah, been looking at skip rules but they don't seem to be working. Testing the skip rules is being triggered but it is still hitting the RewriteRule that it should be skipping. :/
Cole Henley
<htaccess>
htaccess query (not Craft per se)
# htaccess query (not Craft per se)

(Asked by: username_0 on 2015-12-22)

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?

**Tags:** htaccess

> **Comments:**
> **username_1**: Are the domains you're trying to access available exactly as written in your RewriteCond rule, i.e. http://site1.com/ not http://www.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 http://site1.com/news.asp. It should redirect to http://site1.com/news/news-listings?site1.com  
> **username_1**: It could also be as simple as making sure your RewriteCond arguments are wrapped in quotes, i.e. `RewriteCond "%{HTTP_HOST}" "^site1\.com" [NC]`  
> **username_0**: 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 …  
> **username_0**: hmm, run it through http://htaccess.mwl.be/ and it says condition is not being met for domain. most confused  
> **username_0**: 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!  
> **username_1**: Ah yes you do. Multiple RewriteConds only work with a single RewriteRule. There is a 'workaround' here: http://stackoverflow.com/a/11559655/123696  
> **username_1**: Actually, not so sure you can achieve what you need with that. The other alternative is to create a RewriteMap for each domain (see http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewritemap). This will reduce your cond/rule statements down considerably, but could be a little slower per request  
> **username_0**: Thanks Simon - yeah, been looking at skip rules but they don't seem to be working. Testing the skip rules is being triggered but it is still hitting the RewriteRule that it should be skipping. :/  

---

## 1 answers

### Answer by username_1 on 2015-12-22. Score: 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 :)


---

