首页 > 解决方案 > htaccess www -> 非 www 重定向

问题描述

大家,早安!我正在尝试在通过 AWS 托管的 Wordpress 站点中使用 htaccess 来重定向 http -> https 和 www。-> 非 www.,例如:http://www.example.comhttps://example.com

目前,http 到 https 重定向正在按预期工作。然而,“万维网”。没有从我的网址中删除,即http://www.example.com变为https://www.example.com

注意:对于其他重定向,我使用重定向插件。此外,我为 https 使用了真正简单的 SSL 插件。

我已经尝试了重定向代码的多种变体,清除我的缓存,以隐身方式检查它等。

这是我正在使用的当前代码:

  # 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

任何建议将不胜感激!谢谢!

标签: wordpress.htaccessurl-redirectionhttp-redirectno-www

解决方案


你可以这样尝试:

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

保留默认的 WordPress 说明,您应该没有任何问题,请告诉我


推荐阅读