Cain Nuke
Cain Nuke

Reputation: 3079

Modifying my htacess to redirect to SSL URL

My current .htaccess looks like this:

Redirect 301 /~mysite-net/vlog http://www.example.net/vlog
Redirect 301 /~mysite-net/pp https://www.example.net/pp
Redirect 301 /~mysite-net/pp02 https://www.example.net/pp02
Redirect 301 /~mysite-net/pp03 https://www.example.net/pp03
Redirect 301 /~mysite-net/ http://www.example.net/

RewriteEngine On
RewriteRule ^([A-Za-z0-9]+).html$ https://www.example.net/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*?)/?$ index.php?s=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ index\.php\?s=([^\s]*)
RewriteRule ^/?(.*?)/?$ %1?%2%3 [L,R=301]

this does what intended so no problem. However, now I need to modify it so it always redirects to the SSL version of my site. It means no matter if the user types http it will always redirect them to https instead.

So I just added these lines at the bottom:

RewriteCond %{HTTP_HOST} ^example\.net [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.net/$1 [R,L]

but it's not working. What am I missing?

Upvotes: 1

Views: 38

Answers (2)

MrWhite
MrWhite

Reputation: 45829

So I just added these lines at the bottom:

RewriteCond %{HTTP_HOST} ^example\.net [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.net/$1 [R,L]

There are two problems with your existing directives:

  • You have put them in the wrong place in your file - they need to go at the top, not the bottom. By placing them at the end of the file they will only get processed when requesting static resources, because the preceding directives will otherwise "catch" the URL.

  • You are trying to combine the non-www to www redirect in this directive as well, however, you have omitted the OR flag on the first condition, so it will never redirect requests for http://www.example.net/foo.

In other words, at the top of your file.

RewriteCond %{HTTP_HOST} ^example\.net [NC,OR]
RewriteCond %{SERVER_PORT} 80 
RewriteRule (.*) https://www.example.net/$1 [R=301,L]

The RewriteCond %{HTTPS} !on condition mentioned in the other answer is simply an alternative to your SERVER_PORT condition.

Upvotes: 0

Harsh Shah
Harsh Shah

Reputation: 1596

May be this can be work I think you forgot to put RewriteCond %{HTTPS} !on

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

Upvotes: 2

Related Questions