htaccess redirect only for one domain - .htaccess

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]

Related

How do I write these htaccess rules as one rule instead of multiple?

I have these rules in my htaccess. one is for the main domain of the server and the others are other domains that are pointed at the server.
the purpose is to redirect subdomains to a url that is written a certain way. the main domain excludes certain subdomains from the redirect.
RewriteCond %{HTTP_HOST} !^(www|dev|mydev|something|somethingelse)\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-.]+)\.maindomain\.com [NC]
RewriteRule ^(.*)$ https://maindomain.com/index/parser.php?%2&domain_name=maindomain.com [QSA,L]
RewriteCond %{HTTP_HOST} ^(.*)\.anotherdomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-.]+)\.anotherdomain\.com [NC]
RewriteRule ^(.*)$ http://anotherdomain.com/index/parser.php?%2&domain_name=anotherdomain.com [QSA,L]
RewriteCond %{HTTP_HOST} ^(.*)\.whatever\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-.]+)\.whatever\.com [NC]
RewriteRule ^(.*)$ https://whatever.com/index/parser.php?%2&domain_name=whatever.com [QSA,L]
I don't want to keep having to add a new 3 lines for each new domain pointed at the server. How do I create one rule set to incorporate any domain that is pointed at the server? I would still need certain subdomains to be excluded from the main domain, but it wouldn't matter if the same subdomains are excluded from all the domains.
here are a couple things I tried. neither works.
#attempt 1
RewriteCond %{HTTP_HOST} ^(?:www\.|)([a-z0-9-]+)\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteRule ^(.*)$ https://%2/index/parser.php?%1&domain_name=%2 [QSA,L]
#attempt 2
RewriteCond %{HTTP_HOST} !^(www|dev|mydev|something|somethingelse)\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteRule ^(.*)$ https://%2/index/parser.php?%1&domain_name=%2 [QSA,L]
Not sure if I solved it, but this seems to be working. not sure if i can make it even better to handle http and https at the same time, and to handle many exceptions for main domain but just the one exception for the rest, but this is satisfactory enough for my needs. most domains will be ssl, so only need to edit htaccess files to add non ssl domains.
RewriteEngine On
#remove www from any domain pointed at box whether it's ssl or not
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]
#main domain with subdomain exceptions
RewriteCond %{HTTP_HOST} ^(.+)\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^dev\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^mydev\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^something\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^somethingelse\.maindomain\.com$ [NC]
RewriteRule ^ https://maindomain.com/index/parser.php?%1&domain_name=maindomain.com [L,R]
#non ssl domains here. three lines each.
RewriteCond %{HTTP_HOST} ^(.+)\.anotherdomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.anotherdomain\.com$ [NC]
RewriteRule ^ http://anotherdomain.com/index/parser.php?%1&domain_name=anotherdomain.com [L,R]
RewriteCond %{HTTP_HOST} ^(.+)\.whatever\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.whatever\.com$ [NC]
RewriteRule ^ http://whatever.com/index/parser.php?%1&domain_name=whatever.com [L,R]
#all ssl domains use this one rule.
RewriteCond %{HTTP_HOST} ^(.+)\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteCond %{HTTP_HOST} !^www\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteRule ^(.*)$ https://%2/index/parser.php?%1&domain_name=%2 [QSA,L]

SEO, joomla and 2 domains for 2 languages

I’ve been looking around for the answer but I can't seem to find it. This is what I need:
www.domain1.it/en -> www.domain2.com/en
www.domain2.com/it -> www.domain1.it/it
I have tried a lot of possible solutions but no one works
RewriteCond %{HTTP_HOST} www.domain2.com
RewriteCond %{REQUEST_URI} ^/it/ [NC]
RewriteRule .* http://www.domain1.it/it [R=301,L]
It doesn’t work:
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
RewriteCond %{REQUEST_URI} ^/it/(.*)$
RewriteRule ^(.*)$ http://www.domain1.it/$1 [L,R=301]
It doesn’t work:
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/it/(.*)$
RewriteRule ^(.*)$ http://www.domain1.it/$1 [L,R=301]
It doesn’t work:
RewriteCond %{HTTP_HOST} www.domain2.com
RewriteCond %{REQUEST_URI} ^/it/
RewriteRule ^(.*)$ http://www.domain1.it/it/ [R=301,L]
It doesn’t work:
RewriteCond %{HTTP_HOST} ^www\.domain2\.com
RewriteCond %{REQUEST_URI} ^/it/
RewriteRule ^(.*)$ http://www.domain1.it/$1 [R=301,L]
It doesn’t work:
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteCond $1 !^en/$ [NC]
RewriteRule ^(.*)$ http://www.domain1.it/$1
I actually think some of your attempts would work, but your problem is probably your browser caching redirects making your debugging unreliable.
I think the following redirects are simpler and will do what you want.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$ [NC]
RewriteRule ^it(.*) http://www.domain1.it/it$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.domain1\.it$ [NC]
RewriteRule ^en(.*) http://www.domain2.com/en$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]

mod_rewrite redirect of subdomain with subfolder combination

I have this code in my .htaccess to handle subdomains (http://foo.mydomain.com)
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com [NC]
RewriteRule ^(.*)$ filter.php?type=country&country=%1 [L]
The problem is what do i have to do if for example someone puts a link like this:
http://foo.mydomain.com/bar
i want to redirect this to another page different from the filter.php (say otherpage.php) from the previous code, i tried the next code but it isn't working
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com/([^/]+) [NC]
RewriteRule ^(.*)$ otherpage.php?type=country&country=%1 [L]
neither the next one:
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com [NC]
RewriteCond %{REQUEST_URI} ^/([^/]+)
RewriteRule ^(.*)$ otherpage.php?pname=$1 [L]
The link is always resolved with the first rule i wrote.
Add a condition to your first group that checks for an empty REQUEST_FILENAME. This will prevent it from matching when there's something other than a bare domain name.
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com [NC]
RewriteCond %{REQUEST_FILENAME} ^/?$
RewriteRule ^(.*)$ filter.php?type=country&country=%1 [L]

How would I redirect two domains using .htaccess

I'm trying to redirect one domain to the root and another to a directory. The problem I'm having is the second domain is overwriting the firsts redirection.
Here is what I have.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^http://example.site.net$ [NC]
RewriteCond %{REQUEST_URI} !^/.*$
RewriteRule ^(.*)$ / [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^http://example2.com [NC]
RewriteCond %{HTTP_HOST} !^http://www.example2.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{REQUEST_URI} !^/example2_directory/
RewriteRule ^(.*)$ /example2_directory/$1
That's because 'example2.com' and 'www.example2.com' are not 'example.site.net', like your .htaccess rule states...
If you want those rules to apply to those specific domains, you need to remove the '!' in front of them. Otherwise you need to explain what specific domains need to point to what, not just 'one domain' and 'the other domain'.
Edit: Also, you're not supposed to include the 'http://' for your HTTP_HOST conditions.
I think you might not realize you're negating your comparisons with "!"
Also, you dont' have to turn the engine on twice.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^http://example.site.net$ [NC]
RewriteCond %{REQUEST_URI} !^/.*$
RewriteRule ^(.*)$ / [L]
RewriteCond %{HTTP_HOST} ^http://example2.com [NC]
RewriteCond %{HTTP_HOST} ^http://www.example2.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{REQUEST_URI} !^/example2_directory/
RewriteRule ^(.*)$ /example2_directory/$1

Resources