简体   繁体   中英

htaccess wordpress 301 redirect

I recently added SSL to my wordpress site and now if some goes to my http site, I would like to redirect them to my https site. I have tried the following below:

# BEGIN WordPress 
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase / 
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L] 
RewriteRule . /index.php [L] 
</IfModule> 
# END WordPress

But got this error

Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

What am I doing wrong?

Here is the code that you should use for enforcing https:

# BEGIN WordPress 
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase / 

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

RewriteRule ^index\.php$ - [L] 

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

Try this :

RewriteEngine on

RewriteCond %{SERVER_PORT} 80

RewriteRule ^(.*)$ https://{SERVER_NAME}/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.(.*)

RewriteRule ^(.*)$ https://{SERVER_NAME}/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^http://\.(.*)

RewriteRule ^(.*)$ https://{SERVER_NAME}/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} -s [OR]

RewriteCond %{REQUEST_FILENAME} -l [OR]

RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^.*$ - [NC,L]

RewriteRule ^.*$ /index.php [NC,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM