简体   繁体   中英

htaccess www -> non-www redirect

Good morning, everyone! I am trying to use htaccess in my Wordpress site hosted through AWS to redirect http -> https, and www. -> non-www., like so: http://www.example.com to https://example.com .

Currently, the http to https redirect is working as intended. However, "www." is not being stripped from my urls, ie http://www.example.com becomes https://www.example.com

Note: I for other redirects, I use the Redirection plugin. Furthermore I use Really Simple SSL plugin for https.

I have tried multiple variations of the redirect code, clearing my cache, checking it with incognito, etc.

Here is the current code I am using:

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

# END WordPress

Any advice would be greatly appreciated! Thank you!

you could try this way:

###START MOD_REWRITE
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

#REDIRECT ALL www REQUESTS OF YOUR SITE TO THE non-www VERSION AND http -> https
  RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
  RewriteCond %{HTTP_HOST} !(\.dev|staging)
  RewriteCond %{HTTPS} !=on [OR]
  RewriteCond %{SERVER_PORT} 80
  RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

</IfModule>
###END MOD_REWRITE

###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

Leaving the default WordPress instructions you should not have any problem, let me know

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