wordpress htaccess将www重定向到文件夹

时间:2016-09-02 19:06:13

标签: wordpress .htaccess

我有子域sub.main.com由于某种原因被重定向到www.sub.main.com/sub,导致404。

FWIW,这是我的.htaccess文件在根

中的样子
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 
</IfModule>

sub

中的默认wordpress .htaccess

修改

@anubhava提出的

The solution解决了第一个问题,即sub.main.com被重定向到www.sub.main.com/sub,删除了www;但是,这仍然会重定向到sub.main.com/sub

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^main\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

1 个答案:

答案 0 :(得分:1)

将重定向规则保持在最顶层,并使其仅定位到主域:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^main\.com$ [NC]
#RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

确保在测试此更改之前清除浏览器缓存。

编辑:

删除www中的RewriteRule已解决问题。