Gerald Ferreira
Gerald Ferreira

Reputation: 1337

htaccess wordpress https rewrite rule

I am hoping that you can help me with the htaccess rewrite rule below.

# BEGIN WordPress

 # WPhtc: Begin Custom htaccess
 # BEGIN WordPress
 <IfModule mod_rewrite.c>
 RewriteEngine On

 RewriteCond %{SERVER_PORT} !^443$
 RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

 RewriteBase /
 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /index.php [L]
 </IfModule>

 # END WordPress

I have recently changed my wordpress site from http to https... The problem is that old url's redirect to the domain name instead of the https version of that page

eg

if I access the following page https://domain.com/test/testing/ it works 100%, now if I change the https part to http then the page redirects to https://domain.com instead of to https://domain.com/test/testing/ how do I fix it so that if you go to the old version page http://domain.com/test/testing/ (the not https version) that it redirects to https://domain.com/test/testing/ instead of just the domain name https://domain.com

Upvotes: 0

Views: 1417

Answers (4)

Abed Putra
Abed Putra

Reputation: 1215

I use this .htaccess for wordpress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Upvotes: 0

Serge Auxporteurs
Serge Auxporteurs

Reputation: 1

I tried your solution. It worked well, but if you do that you'll need to manually change all your internal links. This works better ;)

RewriteEngine On

RewriteCond %{SERVER_PORT} 80

RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

Upvotes: 0

bumbum
bumbum

Reputation: 1

I've been struggling also with this issue and finally I found a solution for the home redirection and the wordpress in the same htaccess file, and finally it also works for old http links, redirecting to https:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Upvotes: 0

hazzard
hazzard

Reputation: 136

You have to find a workaround for %{REQUEST_FILENAME} since this only represents the file that is accessed. But you obviously want to access the SSL vHost. So you might hardcode the https into your .htaccess.

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

This might help you alot. (found the code above there)

Upvotes: 2

Related Questions