Only the first rule applies in my .htacces and overrides everything else - .htaccess

I need to redirect some websites to another domain. My problem is only the first rule get's applied after the condition and the rest do not work.
Here is an example
RewriteCond %{HTTP_HOST} ^(website\.dk|website-dk|www\.website\.dk) [NC]
RewriteRule ^da/ https://www.website.se/sv/ [L,R=301]
RewriteRule ^da/test1/ https://www.website.se/sv/test1/ [L,R=301]
RewriteRule ^da/test2/foobar/ https://www.website.se/sv/test2/foobar/ [L,R=301]
RewriteRule ^da/test3/foobar123/ https://www.website.se/sv/test3/foobar123/ [L,R=301]
RewriteRule ^da/test4/ https://www.website.se/sv/test4/ [L,R=301]
Here the first RewriteRule works. but if i try the second one i get the same redirect as for the first one.

It is because pattern from ^da/ matches remaining rules also. You may try these refactored rules:
RewriteCond %{HTTP_HOST} ^(?:www\.)?website[.-]dk$ [NC]
RewriteRule ^da/?$ https://www.website.se/sv/ [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?website[.-]dk$ [NC]
RewriteRule ^da/test1/?$ https://www.website.se/sv/test1/ [L,R=301]
RewriteCond %{HTTP_HOST} ^(?:www\.)?website[.-]dk$ [NC]
RewriteRule ^da/test2/foobar/?$ https://www.website.se/sv/test2/foobar/ [L,R=301]
RewriteCond %{HTTP_HOST} ^(?:www\.)?website[.-]dk$ [NC]
RewriteRule ^da/test3/foobar123/?$ https://www.website.se/sv/test3/foobar123/ [L,R=301]
RewriteCond %{HTTP_HOST} ^(?:www\.)?website[.-]dk$ [NC]
RewriteRule ^da/test4/?$ https://www.website.se/sv/test4/ [L,R=301]
Note use of anchor in first rule to get precise matching and alternations in 2nd rules to cover all the cases.

Related

htaccess rewrite - pulling variable from dynamic host

I have a set of mod-rewrite rules which look like this:
RewriteCond %{HTTP_HOST} AAAexample.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.AAAexample.com/folder/AAA [R=301,L]
RewriteCond %{HTTP_HOST} BB-BBexample.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.BB-BBexample.com/folder/BB-BB [R=301,L]
RewriteCond %{HTTP_HOST} CCCCCexample.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.CCCCCexample.com/folder/CCCCC [R=301,L]
Essentially I have a set of domains matching (PATTERN)example.com, where (PATTERN) can contain any combination of letters and hyphens.
Is there a way to condense the above rules into a single set such that I Pull PATTERN as a dynamic variable into the final rewrite URL?
Thanks!
You can use:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)(example\.com)$ [NC]
RewriteRule ^/?$ http://www.%1%2/folder/%1 [R=301,L]

htaccess redirect only for one domain

I have several domains pointing to the same webspace (www.domain1.com, www.domain2.com, www.domain3.com, etc.). Only when one domain is used, e.g. www.domain1.com, I want to redirect users from different short link, e.g. www.domain1.com/link or www.domain1.com/link2, to another URL.
I have put together the following. The RewriteCond is probably ok but the RewriteRules doesn't work:
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com [NC]
RewriteRule ^/test$ http://www.domain1.com/xyz.php [R=301]
RewriteRule ^/test2$ http://www.domain1.com/abc.php [R=301]
RewriteRule ^/test3$ http://www.domain1.com/abc/test10.php [L,R=301]
Do you have any tip how the correct RewriteRules should look like?
Hopefully this helps you get going in the right direction...
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com [NC]
RewriteCond %{REQUEST_URI} ^(/test/)$ [NC]
RewriteRule ^(.*)$ http://www.domain1.com/xyz.php [R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com [NC]
RewriteCond %{REQUEST_URI} ^(/test2/)$ [NC]
RewriteRule ^(.*)$ http://www.domain1.com/abc.php [R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com [NC]
RewriteCond %{REQUEST_URI} ^(/test3/)$ [NC]
RewriteRule ^(.*)$ http://www.domain1.com/abc/test10.php [L,R=301]

redirect domains and subfolder to new domain htaccess

I have 4 domain which I want 3 of them redirect to last one. I already used some htaccess rules and they are working great,
RewriteEngine On
RewriteCond %{HTTP_HOST} ^first.com$ [NC]
RewriteRule ^(.*) http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.first.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^second.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.second.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.third.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^third.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
the only problem is when I enter "www.first.com/about" it does not shows "www.forth.com/about" just shows the same thing . all the domain are forwarded to forth.com and they do not have hosting so I cannot put htaccess file for those other domains , please guide. would be appreciated.
This is most likely what is happening, at least on my test cases this fix handles it.
Instead of using [L,R=301] reverse the order and do [R=301,L]. The L means Last and it ends cycle on evaluating what to do at that point. I've always used [R=301,L] as are all the examples that I could find anywhere. Potentially your apache may be hitting that L and immediately pass back the new string to the URL parser and isn't sending it as a 301 Redirect as it didn't get that far.
Also on the first.com snippit above you left out the $ on the ^(.*) - only for that line though, not sure if this was a typo or if it's missing in your actual .htaccess file.
Otherwise, you could tidy it up a bit by using:
RewriteCond %{HTTP_HOST} ^((www.)?)first.com [NC,OR]
RewriteCond %{HTTP_HOST} ^((www.)?)second.com [NC,OR]
RewriteCond %{HTTP_HOST} ^((www.)?)third.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [R=301,L]

ModRewrite canonical URLs in .htaccess file

I'm using ModRewrite to redirect URLs to their canonical ones in my .htaccess file. I've got something a bit like this:-
RewriteCond %{HTTP_HOST} ^www.ex\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.ex\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^ex\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
It works, but doesn't look pretty. Can I combine these conditions into a single rule?
You can link the conditions together by including an OR in the brackets, since the Rule that they are tied to are all the same:
RewriteCond %{HTTP_HOST} ^www.ex\.co\.uk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.ex\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^ex\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

.htaccess RewriteRule non-www to www without explicity specifying domain

I'm trying to make a general rewrite rule to redirect all domain.com requests to www.domain.com.
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+)\.([0-9a-z-]+])$ [NC]
RewriteRule ^(.*)$ http://www.{HTTP_HOST}/$1 [R=301,L]
The problem is that this rewrite rule doesn't match anything. How can I change it? Thanks
I guess the problem is that there’s an additional ] in your RewriteCond’s pattern and that there is a % missing when referencing HTTP_HOST in RewriteRule’s substitution. So try this:
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+)\.([0-9a-z-]+)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
And to cover Cags’ concerns a litte bit, you can also try this rule:
RewriteCond %{HTTP_HOST} ^[^./]+\.[^./]+$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^./]+)\.[^./]+\.[^./]+$ [NC]
RewriteCond %1 !=www [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Resources