I want to redirect every request do https://www.example.com/request
It's already working, but I want to minimize the redirects. Currently I got 2 redirects: http://example.com -> https://example.com -> https://www.example.com
Every other case is working with 0 or 1 redirect.
My .htaccess contains the following (generated by "Easy HTTPS (SSL) Redirection" WordPress Plugin):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
Got it for my own
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^example\.com.* [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com.*
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]
Related
I am currently using the folowing htaccess to redirect forcefully to https. But this is not redirected to https when the URL is http://www.myexample.com.
How can I change to :
http://www.myexample.com -> https://www.myexample.com
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^myexample\.com [NC]
RewriteCond %{HTTPS} !on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.myexample.com/$1 [R=301,L]
</IfModule>
I added an SSL to my site and have the following htaccess file in place to redirect:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*)$ "https://example.com/$1" [R=301,L]
</IfModule>
This covers almost everything, so http://www.example.com, http://example.com, and example.com all forward to https://example.com. However, there is one scenario where it does not. If I enter https://www.example.com, it does not redirect to https://example.com and I get a security certificate error. Looking for a solution. Thank you in advance.
cdr6545
Try this
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymlinks
# To redirect from www to non www (Rewrite www.example.com → example.com)
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http%{ENV:protossl}://%1/$1 [R=301,NE,L]
# Redirect HTTP to HTTPS automatically (only if not in localhost)
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
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]
I have a problem with .htaccess
I have a certificate in my server, and it's only for https://example.com (not https://www.example.com) Therefore, I'm trying to do a .htaccess redirect to the correct url.
My intention is this:
http://www.example.com --> https://example.com
http://example.com --> https://example.com
https://www.example.com --> https://example.com
I tried different combinations and nothing seems to work. At the moment I have this, but seems like is not working for
http://www.example.com/subfolder, I don't know what is failing...
RewriteEngine On
# Follow symbolic links.
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# Prevent directory listings
Options All -Indexes
That code is not working for me, I found that, which works for me
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Replace your current code by this one
Options -Indexes +FollowSymLinks
RewriteEngine On
# redirect "www" domain to https://example.com
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# redirect http to https (at this point, domain is without "www")
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$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.