Combining Rules in .htaccess file? - apache

I'm wondering how to connect these two rewrite rules in one .htaccess file. Currently, I force https connections to my website with this rule:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{ENV:HTTPS} !=on
RewriteCond %{HTTP_HOST} !=SERVER_NAME
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Now I want to redirect from my www-subdomain to my real domain. There is a high rated answere here:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
But I fail to put these two together.

You can use:
RewriteEngine On
# if with www, redirect to https domain without www
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
# if http, redirect to https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
If you test first for www and redirect to https, no need to test for http://www.
Or like #PanamaJack said, you just can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://domain.com%{REQUEST_URI} [NE,L,R=301]
But you need to change the domain name.

Related

rewrite/redirect http:// and http://www to https:// using htaccess

I'm new to editing htaccess files. What I want to do is:
rewrite/redirect specific subdomains eg subdomain1, subdomain2, etc from:
https://www.subdomain.example.com
http://www.subdomain.example.com
http://subdomain.example.com
To:
https://subdomain.example.com
I've pieced the below code together from other questions, but it doesn't seem to be working.
Can anyone explain what I'm doing wrong and help me fix the code.
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
To canonicalise ("redirect") only the specific subdomain ...
https://www.subdomain.example.com
http://www.subdomain.example.com
http://subdomain.example.com
To:
https://subdomain.example.com
You would need to do it something like this near the top of your .htaccess file:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(subdomain\.example\.com) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
The 3rd condition ensures that it only applies to the specific subdomain.
To make this apply to several subdomains (assuming the no-www is canonical for all subdomains) then you could modify just the 3rd condition to identify the subdomains that it should apply to. For example:
RewriteCond %{HTTP_HOST} ^(?:www\.)?((?:subdomain1|subdomain2|subdomain3)\.example\.com) [NC]
Test with a 302 (temporary) redirect to avoid potential caching issues. You will need to clear your browser cache before testing.
RewriteCond %{HTTPS} !on
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This is along the right lines, but it fails to target HTTPS or the www sub-subdomain. Also, checking %{HTTPS} !on and %{SERVER_PORT} ^80$ are really the same thing.
You can use the following :
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R,L]
This will redirect all subdomains from http://www to https:// .
For the subdomain.example.com you can use the following :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule ^ https://subdomain.example.com%{REQUEST_URI} [R,L]

Force www and redirect root directory

I couldn't find a question that answered mine, or maybe I couldn't search using the right terms, but come on. I have the following rule in my htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com\.br$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\.br$
RewriteRule ^(.*)$ "https\:\/\/www\.domain\.com\.br\/site" [R=301,L]
This works when the user enters only the URL (domain.com.br or www.domain.com.br), but I need it to redirect when the user accesses this way:
domain.com.br or www.domain.com.br --> https://www.domain.com.br/site
domain.com.br/XXX --> https://www.domain.com.br/XXX
What rule should I use for this?
Update: the server already has a default rule to force SSL, in which case it is unnecessary to put it in htaccess
Rule update:
**On virtual host:**
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
**On htaccess file:**
RewriteCond %{HTTP_HOST} ^domain\.com\.br$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\.br$
RewriteRule ^/?$ https://www.domain.com.br/site/ [R=301,L]
Your code generates a redirect loop. Also, you don't need to escape slashes on targets.
You could merge everything inside your virtual host block (faster than using a htaccess):
RewriteEngine On
# force https
RewriteCond %{HTTPS} off [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# force www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# redirect root directory to /site/
RewriteRule ^/?$ /site/ [R=301,L]
Of course, you will see a redirect chain for e.g. http://domain.tld/ as it will first redirect to https://domain.tld/ then to https://www.domain.tld/ and finally to https://www.domain/tld/site/. This is fine. However, if you really want to handle everything with only one redirect, you could. But, it will be less generic.
For instance, you would end up with those rules:
RewriteEngine On
# force root directory to /site/ (https+www)
RewriteRule ^/?$ https://www.domain.com.br/site/ [R=301,L]
# force any other pages to https+www if not the case already
RewriteCond %{HTTPS} off [NC,OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.com.br%{REQUEST_URI} [R=301,L]

Can't redirect to https (htaccess)

I was trying to configure my htaccess file which will redirect from http to https with following lines of statement. But it couldn't .
Note: i don't want the www before the site URL as well.
What could be possible error of my code. Anyone please suggest.
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,L]
Problem appears to be this line:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
This line makes this rule work for domain with starting www only.
You need to make starting www optional by using:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
As another solution, if you have only one domain name (www.)example.com, you can use more readable:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]
Try the following:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
Also, you can also redirect based on port number, for example:
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
This will redirect all requests received on port 80 to HTTPS.

.htaccess allow one url to be accessed either via HTTPS or HTTP

I have a rewrite rule in .htaccess that redirects our site to HTTPS
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I need to leave one url open to work both HTTP and HTTPS (http://www.myhost.com/api and https://www.myhost.com/api). How do I need to modify my above code
Try below rule,
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/support/registration_service
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Above rule excluding the uri /api to be forced to https so you can use www.myhost.com/api for both scheme.

htaccess RewriteRule to https doesn't work combined with other rules

I'm trying to set a few rules in my .htaccess to work in a specific way:
Specific page redirections:
RewriteRule "^web/site/mais$" https://www.main.com/front/for-you/insurance/ [R=301,L]
Home redirection
RewriteRule ^$ https://www.forexample.com.br/for-you/ [R=301,L]
RewriteRule ^/$ https://www.forexample.com.br/for-you/ [R=301,L]
HTTP to HTTPS and add www to the rest (this is what doesn't work), i tried many things:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS} !^on$ [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS} !^on$ [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Other way I tried forgotting the www rule (but doesn't work too):
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The only way I found to combine is writting the general https and www rule in the httpd.conf and the other rules into the .htaccess, but this way I get two or more 301 redirects, bad business for SEO.
Do you have the same problema with all the directories or only with some?
Because if you have ProxyPass (as you said), maybe it send you the redirections directly to Tomcat, so maybe you need to specify the rules into the ssl.conf to read that rules before leave the ssl.conf. Try to write the last you wrote into the ssl.conf after the ProxyPass
If you want to redirect some spacific pages to https, forexample, To redirect :
http://example.com/page.html
to
https://www.example.com/page.html
And
http://example.com/page2.html
to
- https://www.example.com/page2.html
You may try the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(page|page2)\.html$ https://www.example.com/$1.html [L,R]
If you want to redirect the whole site from http to https, you can use the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [NC,L,R]