Multiple domains on one host https and http - apache

I have multiple domains on one host, my .htaccess looks like this
RewriteEngine On
RewriteCond %{HTTP_HOST} web1.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web_1/public/.*$
RewriteRule ^(.*)$ /web_1/public/$1 [L]
RewriteCond %{HTTP_HOST} web2.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web_2/public/.*$
RewriteRule ^(.*)$ /web_2/public/$1 [L]
And it works so far. Now I want only web1.com, not web2.com, to redirect to https. How do I have to change the settings? (I am also thankful for tips about how to change my "implementation" so far, I am new to .htaccess and willing to learn more about it)

Try this :
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} web2.com$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
So , your rules will look like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} web1.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web_1/public/.*$
RewriteRule ^(.*)$ /web_1/public/$1 [L]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} web2.com$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} web2.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web_2/public/.*$
RewriteRule ^(.*)$ /web_2/public/$1 [L]

Related

htaccess redirect to https exclude one url

I know its duplicate and there are a lot of answers in google, but nothing works for me, yet.
I'm not strong or even average with apache, but I'd like to exclude one url that starts from /test/ from HTTPS redirect.
//redirect not working
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^\/(/test/)
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteCond %{HTTP:X-Forwarded-Proto} https
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^\/(/test/)
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
//infinity loop
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{REQUEST_URI} ^\/(test)
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} !^\/test)
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
//thats my first HTTPS redirect, tried to add RewriteCond to exclude test, but nothing works
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/test/ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
I'd appreciate for the help.
Found an answer by myself
# force https:// for all except some selected URLs
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/test/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force http:// for selected URLs
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /test/ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Force HTTPS for only a couple URLs?

I have tried every single thing I've come across to get only 2 specific URLs to redirect to https. NOTHING has worked. Here's what I have right now in .htaccess
RewriteCond %{REQUEST_FILENAME} topdeal(.*)
RewriteCond %{REQUEST_FILENAME} watchstore(.*)
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Try this rule with THE_REQUEST variable instead of REQUEST_FILENAME:
RewriteCond %{THE_REQUEST} watchstore|topdeal [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
You can use the following in your .htaccess file. What the below does is force everything to HTTP except for the directories watchstore and topdeal. Both are forced to HTTPs.
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{REQUEST_URI} ^\/(watchstore|topdeal)
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} !^\/(watchstore|topdeal)
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
Make sure you clear your cache before testing this.
EDIT:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^\/(watchstore|topdeal)
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^\/(watchstore|topdeal)
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

.htaccess Rewrite modification

We're currently using this rule:
RewriteRule ^testsub/ - [L]
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.website\.com$
RewriteRule ^(.*)$ testsub/$1 [L]
But, we're unable to to easily have more rules to rewrite subdomains to seperate folders.
Copying and pasting means all subdomains are going to testsub.website.com
If I add the following condition I can get around it:
RewriteCond %{HTTP_HOST} !^(test|alt)\.website\.com$ [NC]
But as you can see from below it's still a big, ugly rewrite... how can I easily make just 1 set of rules to do all of the below as like 4-5 lines max
RewriteRule ^testsub/ - [L]
RewriteCond %{HTTP_HOST} !^(test|alt)\.website\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.website\.com$
RewriteRule ^(.*)$ testsub/$1 [L]
RewriteRule ^test/ - [L]
RewriteCond %{HTTP_HOST} !^(testsub|alt)\.website\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.website\.com$
RewriteRule ^(.*)$ test/$1 [L]
RewriteRule ^alt/ - [L]
RewriteCond %{HTTP_HOST} !^(testsub|test)\.website\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.website\.com$
RewriteRule ^(.*)$ alt/$1 [L]
You can use like this
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.website\.com$
RewriteRule ^(testsub|test|alt)(.*)$ $1/$2 [L]
I hope that helps!

.htaccess redirect subdomain before forcing HTTPS?

I have the following rules on my website:
Force HTTPS on all pages.
Redirect http://user.domain.com to https://www.domain.com/file.php?user=user
The problem I'm having now is that when someone goes to http://user.domain.com , they get redirected to https://user.domain.com , which is then insecure, instead of to https://www.domain.com/file.php?user=user .
How do I fix this?
Here is the .htaccess content:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ - [L]
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com/file.php?user_id=%1 [L]
I also tried to change %{HTTP_HOST} to %{HTTPS_HOST} and it ended up with the same problem.
Try with:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^.* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com/file.php?user_id=%1 [L]
You can use these rule:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^ /file.php?user_id=%1 [L,QSA]

Keep referer mod_rewrite

Please help with the following question.
There is domain1.com, domain2.com, domain3.com. With the help of organized htaccess redirects to domain.com. But in the apache logs for domain.com, I do not see the referer, since when domain1 or domain2 request came.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com?$ [NC]
RewriteRule ^$ http://domain.com/$1 [R=302,L]
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^rss\.domain1\.com$ [NC]
RewriteCond %{HTTP_HOST} !^contacts\.domain2\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=302,L]
RewriteCond %{HTTP_HOST} ^rss.domain1.com$
RewriteRule ^/?$ "http\:\/\/www\.google\.com" [R=302,L]
RewriteCond %{HTTP_HOST} ^contacts.domain2.com$
RewriteRule ^/?$ "https\:\/\/www\.google\.com" [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ / [R=302,L]