Thomas Jackson February 2016

Redirect to a HTTPS version using .htaccess

I need to modify my .htaccess file to redirect all URLs to a HTTPS version without the "www".

http://example.com --> https://example.com
http://www.example.com --> https://example.com
https://www.example.com --> https://example.com

This is how my .htaccess file looks like:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ redirect.php?id=$1&page=$2 [QSA,L]

What modifications do I need to make to .htaccess in order to forward to HTTPS without the "www"?

Answers


Panama Jack February 2016

It depends on what you are doing with your current code. That just looks like a file that is called redirect but is trying to display a php page from a SEO friendly URL. So I think that might be confusing people.

Anyway, to do what you need to do and force https without www, you only need one more rule above.

Also your second rule needs two capture groups in the RewriteRule test string because you are wanting two different values using $1 and $2 back references.

RewriteEngine On

#rediect www to non www and/or http to https --- all combinations.
RewriteCond %{HTTP_HOST} !^example\.com [NC,OR]
RewriteCond %{HTTPS} !^on
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.+)/?$ redirect.php?id=$1&page=$2 [QSA,L]

So this second rule will work for an URL formed like this.

https://example.com/123/pagename

Post Status

Asked in February 2016
Viewed 2,875 times
Voted 4
Answered 1 times

Search




Leave an answer