htaccess force https & www. on domain & http no www on subdomain - apache

How do I get in htaccess to force https & www. on domain, & http no www on subdomain?
The following redirects the http://sub.domain.com to https://domain.com
# for main domains
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com%{REQUEST_URI} [R,L]
# for sub domain
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com$ [NC]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ http://sub.domain.com%{REQUEST_URI} [R=301,L,NE]

You can use:
# for main domains
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L,NE]
# for sub domain
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com$ [NC]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ http://sub.domain.com%{REQUEST_URI} [R=301,L,NE]
You need to test this after clearing your browser cache.

Related

How to disable https redirect on a single page with htaccess

I am trying to disable https on a page on my site which also have a dynamic sub pages. For example http://example.com/page/dynamic/section.
I have try below htacess code
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/page/* [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But still get stuck and it redirecting to https please any one?
I am trying to disable https on a page on my site which also have a dynamic sub pages. For example http://example.com/page/dynamic/section:
You may use this .htaccess code:
RewriteEngine On
# add www if missing to all the URIs
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# turn on https except for a specific URI
RewriteCond %{HTTPS} !on
RewriteCond %{THE_REQUEST} !/page[/\s?] [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# force http for a specific page:
RewriteCond %{HTTPS} on
RewriteRule ^page(/|$) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE,NC]

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]

Htaccess on Forwarding http to https plus forcing www

I use the following code in my .htaccess
RewriteCond %{HTTPS} off
RewriteRule !^(index(\.php)?)?$ https://%{HTTP_HOST}/$1 [R=301,L]
this work well for https when i add
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS} off
RewriteRule !^(index(\.php)?)?$ https://www.%{HTTP_HOST}/$1 [R=301,L]
to force www infront it works only to force www infront but does not forward http to https.
any help on this
To enforce www and htpp->https in same rule use an OR between 2 conditions:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^ http://www.%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{THE_REQUEST} !/index\.php [NC]
RewriteRule . https://www.%1%{REQUEST_URI} [R=301,L,NE]
3rd RewriteCond is for capturing value of domain after www.

Wildcard 301 redirect using .htaccess

I moved a website from http://lifeworkslearningcenter.com to http://lifeworks.life. I set up seven 301 redirects for individual URLs that are working fine, as well as a simple 301 to redirect the base URL to the new base URL.
Now I need to set up a wildcard for the remaining URLs, typos, etc. What is happening right now is this: any URL not specifically covered in the 7 redirects will redirect to the new base URL, then add the previously entered trailing URL, and results in a 404. Like this:
http://lifeworkslearningcenter.com/incorrect-url
https://www.lifeworks.life/incorrect-url
Here is my .htaccess code, including three previous attempts at wildcard redirect statements:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^about-lifeworks$ "https\:\/\/www\.lifeworks\.life\/team\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^services$ "https\:\/\/www\.lifeworks\.life\/services\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^methodology$ "https\:\/\/www\.lifeworks\.life\/approach\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^sign-up$ "https\:\/\/www\.lifeworks\.life\/enroll\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^rates-and-policies$ "https\:\/\/www\.lifeworks\.life\/policies\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^contact-us$ "https\:\/\/www\.lifeworks\.life\/contact\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
RewriteRule ^blog$ "https\:\/\/www\.lifeworks\.life\/blog\.html" [R=301,L]
Redirect 301 / http://lifeworks.life/
# FAILED ATTEMPTS AT WILDCARD REDIRECT
#RewriteCond %{HTTP_HOST} ^lifeworkslearningcenter\.com$ [OR]
#RewriteCond %{HTTP_HOST} ^www\.lifeworkslearningcenter\.com$
#RewriteRule ^$ "https\:\/\/www\.lifeworks\.life\/index\.html" [R=301,L]
#RedirectMatch 301 /(.*) /$1
#RedirectMatch ^/(.*)$ http://lifeworks.life/$1
Your wildcard redirect should look like this and placed at the bottom of your other rules.
RewriteRule ^(.*)$ https://www.lifeworks.life/$1 [R=301,L]
If you need it to redirect based on file not found (404) you can have these rules. And place these at the bottom of your rules.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://www.lifeworks.life/$1 [R=301,L]

.htaccess rules causing internal server error yet they look right

My server's. htaccess rules look like this:
RewriteEngine On
# first redirect
RewriteCond %{HTTP_HOST} ^www\.testing\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^testing\.examplewebsite\.co\uk$ [NC]
RewriteRule ^(.*)$ https://testing.examplewebsite.com/$1 [L,R=301]
# second redirect
RewriteCond %{HTTP_HOST} ^www\.test\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^test\.examplewebsite\.co\uk$ [NC]
RewriteRule ^(.*)$ https://test.examplewebsite.com/$1 [L,R=301]
# Main redirect
RewriteCond %{HTTP_HOST} ^www\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^examplewebsite\.co\uk$ [NC]
RewriteRule ^(.*)$ https://examplewebsite.com/$1 [L,R=301]
# All subdomains that don't exist redirect
RewriteCond %{HTTP_HOST} ^(.+)\.examplewebsite\.co\.uk$ [NC]
RewriteRule ^ https://www.examplewebsite.com/ [L,R]
But for some reason none it causes a server internal error, I'm not sure if they are all 100% correct either. Like the last rule, should that too use ^(.*)$ at the start and $1 at the end
Same for the flags, not too sure if they are all correct.
Basically I am trying to make:
testing.examplewebsite.co.uk and www.testing.examplewebsite.co.uk go to https://testing.examplewebsite.com
test.examplewebsite.co.uk and www.test.examplewebsite.co.uk go to https://test.examplewebsite.com
Some other subdomains like above
examplewebsite.co.uk and www.examplewebsite.co.uk go to https://examplewebsite.com
Make all used domains, for example stackoverflow.examplewebsite.co.uk and www.stackoverflow.examplewebsite.co.uk both be sent to https://examplewebsite.com
You have a typo in all of your conditions. the 2nd conditions in your lines is missing a . before uk. ^testing.examplewebsite.co.uk$ [NC]
RewriteEngine On
# first redirect
RewriteCond %{HTTP_HOST} ^www\.testing\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^testing\.examplewebsite\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://testing.examplewebsite.com/$1 [L,R=301]
# second redirect
RewriteCond %{HTTP_HOST} ^www\.test\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^test\.examplewebsite\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://test.examplewebsite.com/$1 [L,R=301]
# Main redirect
RewriteCond %{HTTP_HOST} ^www\.examplewebsite\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^examplewebsite\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://examplewebsite.com/$1 [L,R=301]
# All subdomains that don't exist redirect
RewriteCond %{HTTP_HOST} ^(.+)\.examplewebsite\.co\.uk$ [NC]
RewriteRule ^ https://www.examplewebsite.com/ [L,R]