htaccess different 404 on wordpress subfolder

时间:2016-04-15 15:09:32

标签: php wordpress .htaccess redirect

I have a Wordpressinstallation at the sub-folder www.example.com/blog of my domain. It has it's own 404 page which unfortunately also shows if a page can't be found on my main website.

I'd like to have a 404 redirection of my main Website and the Wordpress one for my blog in the subfolder.

This is the code I have until now. But the second one is overwriting the first. How can I make the wordpress redirection only apply to the subfolder?

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteRule ^(.*) http://www.example.com/$1 [R=301,NE,L]

ErrorDocument 404 /notfound/404.html

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

1 个答案:

答案 0 :(得分:0)

好的,如果你的主网站是www.domain.com而你的WP博客是www.domain.com/blog那么为什么WP和非WP规则都在同一个.htaccess文件中? 我的意思是,你的子文件夹中的WP应该在同一个子文件夹中有.htaccess,而主网站在根文件夹中有不同的.htaccess。

编辑:假设主网站不是WP,您应该有2个不同的.htaccess文件。第一个使用非WP规则进入主网站目录,如下所示:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*) http://www.domain.com/$1 [R=301,NE,L]
ErrorDocument 404 /notfound/404.html

第二个是普通的WP .htaccess文件,然后转到WP目录:

RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]

这样就可以了。