How do I force www to use https and not just http?
For example, when visiting http://www.example.com it redirects to https://www.example.com but if you don't include the www, you don't get redirected to the SSL version.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.(.*)$
RewriteCond %{REQUEST_URI} !^/admin
RewriteCond %{REQUEST_URI} !^/crossdomain.xml
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Below condition should work for both www and without it.
RewriteCond %{HTTP_HOST} ^(www\.)?example.com [NC]
Related
I have this htaccess:
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteCond %{REQUEST_URI} !^/([a-zA-Z0-9-z\-]+)
RewriteRule ^(.*)$ https://example.com/subfolder/%1 [L]
It is redirecting to https and also .example.com to example.com/subfolder/ ,
but when I open some subdomain, it gives me a certificate error.
Can I prevent this somehow?
main domain should redirect to https
all subdomains should redirect without ssl to the https://example.com...
thank you
I want to 301 redirect the following URL’s via htaccess file
Which means I need to force SSL(https) version and force the www across the whole website.
In addition I also want to 301 redirect the index.html to the https and www version of the homepage url.
I want to 301 redirect the following urls:
https://www.example.com/index.html
https://example.com/index.html
https://example.com
http://www.example.com/index.html
http://example.com/index.html
http://www.example.com
http://example.com
www.example.com
example.com
To
“https://www.example.com”
In addition to that I need all urls of the site to be redirected to https://www version like this:
“https://www.example.com/new-site.html”
Heres what I am using right now:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
RewriteBase /
RewriteRule ^index\.html$ / [NC,R,L]
But the above rules seem to give me a 301--->302-----200 ok redirect for 2 url's with /index.html and 301 for the rest.
It gives a 302 from:
“https://www.example.com/index.html”
To this
“https://www.example.com/“
Thank you.
You can do all this in a single rule:
RewriteEngine On
## add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{REQUEST_URI} /index\.html$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule (?:^|(.+)/)(?:index\.html)?$ https://www.%1/$1 [R=301,L,NE]
Ok so I figured it out myself:
Here's how it will work, just needed to add R=301 in the last rule and not just R
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
RewriteBase /
RewriteRule ^index\.html$ / [NC,R=301,L]
I want to redirect my SSL site (domain.com) with HTTPS protocol but not domain.org which is actually a sub-directory of domain.com (domain.com/domainorg/). I want this:
http://domain.com TO https://www.domain.com
http://domain.org TO http://www.domain.org
I tested my htacess on http://htaccess.mwl.be/ and it works fine but when I actually run it, I get “The page isn't redirecting properly” and it appears to be in a loop when I test all options.
This is my htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www.)?domain.org$
RewriteRule ^/?(.*)$ http://www.domain.org/$1 [L,R=301]
RewriteRule ^/?(.*) https://www.domain.com/$1 [L,R=301]
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
</IfModule>
I am not proficient at htacess at all. What do I need to correct or change?
Why are some of your rules outside <IfModule mod_rewrite.c>? I'd put all of them inside it.
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirecting domain.com http URLs to https (any non-www will be rewritten to www as well)
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
# Redirecting domain.com non-www domain.com URLs to www (any non-https will be rewritten to https as well)
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
# Redirecting domain.org non-www URLs to www (any https will be rewritten to http as well)
RewriteCond %{HTTP_HOST} ^domain\.org$ [NC]
RewriteRule ^(.*)$ http://www.domain.org/$1 [R=301,L]
# Redirecting domain.org https URLs to http (any non-www will be rewritten to www as well)
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
RewriteRule ^(.*)$ http://www.domain.org/$1 [R=301,L]
</IfModule>
You may also force URLs like this domain.com/domainorg/* to be redirected to http://www.domain.org/* using:
RewriteCond RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond RewriteCond %{REQUEST_URI} ^\/?domainorg(\/.*)?$ [NC]
RewriteRule ^.*$ http://www.domain.org%1 [R=301,L]
Here is my current .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
</IfModule>
I want to redirect all http requests to https, and all www requests to non-www, and all file requests to index.php
For example:
http://www.example.com to https://example.com
https://www.example.com to https://example.com
http://example.com/file.php to https://example.com/index.php
Everything seems to be working except the www part.. Any help please?
http://www.example.com to https://example.com
https://www.example.com to https://example.com
http://example.com/file.php to https://example.com/index.php
Maybe this will work:
RewriteEngine On
# Remove www from https requests.
# Next 3 lines must be placed in one .htaccess file at SSL root folder,
# if different from non-ssl.
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,L]
# Redirect all http to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (?:www\.)?(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,L]
If it doesn't work, try replacing
RewriteCond %{HTTPS} off or on with
RewriteCond %{HTTP:X-Forwarded-SSL} off or on
You can add an additional rule dealing with the www part
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [L,R]
The RewriteCond captures everything after the www. and uses that in the RewriteRule as %1.
When everything works as you expect, you can change R to R=301.
Never test with 301 enabled, see this answer
Tips for debugging .htaccess rewrite rules
for details.
I have this site, example.com. What I want to do is force www on all pages, force https on one page, force http on all other pages.
I am using the following .htaccess code to redirect all http traffic to www:
#Redirect to www
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
and the following to set SSL on my "form" page:
#Force SSL on a specific directory
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteCond %{REQUEST_URI} form
RewriteRule ^(.*)$ https://www.example.com/form/$1 [R,L]
Problem is, when I hit https://example.com/, it does not redirect to www (https://www.example.com), and especially not http and www (http://www.example.com) like I want it to. I tried this:
#Redirect SSL to www
RewriteCond %{ENV:HTTPS} on [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [R=permanent,L]
but that was a really dumb attempt and didn't work. How can I accomplish all three at once?
Try this:
# Handle nonSSL
RewriteCond %{HTTPS} on
RewriteRule !^/?form(/|$) http://www.ravicti.com%{REQUEST_URI} [L,R=301]
# Handle SSL
RewriteCond %{HTTPS} off
RewriteRule ^/?form(/|$) https://www.ravicti.com%{REQUEST_URI} [L,R=301]
# Add the missing www for both HTTP and HTTPS
RewriteCond %{HTTP_HOST} ^ravicti.com$ [NC]
RewriteCond %{HTTPS}:s (on:(s)|off:s)
RewriteRule (.*) http%2://www.ravicti.com%{REQUEST_URI} [L,R=301]