Multiple RewriteCond doesn't work - .htaccess

I have in htaccess some like this:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !^/([a-z]{2})
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/en [L,R=301]
I want redirect if non-www and non-lang
What is wrong?

You actually need a OR instead of default AND.
Have it like this:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !^/[a-z]{2}
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^(?:[a-z]{2}/)?(.*)$ https://www.%1/en$1 [L,R=301]

Thanks anubhava, your reply has helped me to find the final answer to my case.
I finally solved using this way:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(?:[a-z]{2})?(.*)$ https://www.%{HTTP_HOST}/en [L,R=301]

Related

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]

Unique page http to https redirect by .htaccess

I've got this code in .htaccess now:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} (.+)$
RewriteRule ^(.*) http://www.%1/$1 [L,R=301]
Now I need my cgi script cgi-bin/test.cgi redirected to https://www.example.com/cgi-bin/test.cgi
I've already tried
RewriteRule ^cgi-bin/test.cgi$ https://www.example.com/cgi-bin/test.cgi [L]
and some more other variation but it doesn't work
Please help!
Try:
RewriteCond %{HTTPS} off
RewriteRule ^cgi-bin/test.cgi$ https://www.example.com/cgi-bin/test.cgi [L,R]

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]

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]

Rewrite rule for dynamic subdomain redirecting each file with subdomain arguments

I want to redirect all the calls to files in a subdomain with an extra variable. like if a user access www.domain.com/news.php and in the subdomain he accesses the same page then it should add an extra argument to the url like xyz.domain.com/news.php should be re written like it calls the file news.php?subdomain=xyz. Also i have other rules for just simple domain.
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule ^$ /index.php?subdomain=%1 [L]
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?subdomain=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?%{QUERY_STRING}&subdomain=%1 [L]
The solution pointed to by adaptr at: https://serverfault.com/a/409171/128746 - would solve this problem very well.

Resources