【问题标题】:htaccess different 404 on wordpress subfolderhtaccess wordpress子文件夹上的不同404
【发布时间】:2016-04-15 15:09:31
【问题描述】:

我在我的域的子文件夹 www.example.com/blog 中有一个 Wordpressinstallation。它有自己的 404 页面,不幸的是,如果在我的主网站上找不到页面,它也会显示。

我想要一个404 重定向我的主网站和子文件夹中我博客的 Wordpress 重定向。

这是我到现在为止的代码。但是第二个覆盖了第一个。如何使 wordpress 重定向仅适用于子文件夹?

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>

【问题讨论】:

    标签: php wordpress .htaccess redirect


    【解决方案1】:

    好的,如果您的主要网站是 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]
    

    这样就可以了。

    【讨论】:

    • 嗯,好的,我已将 Wordpress 代码放入 /blog。但是我的博客然后停止工作。那你知道我需要怎么改代码吗?
    • 好的,你能把主网站文件夹和WP博客文件夹中的.htaccess的内容贴出来吗?
    • 所以前 4 行用于主网站:RewriteEngine On RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC] RewriteRule ^(.*) http://www.domain.com/$1 [R=301,NE,L] ErrorDocument 404 /notfound/404.html 另一部分用于 Wordpress:&lt;IfModule mod_rewrite.c&gt; RewriteEngine On RewriteBase /blog/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /blog/index.php [L] &lt;/IfModule&gt;
    • 好的,我不知道为什么,但是我用 wordpress 设置中的相同代码覆盖了 .htaccess 文件,现在它可以工作了……我在代码中找不到任何区别。
    • 那么你说的到底对你有用吗?
    猜你喜欢
    • 2012-09-14
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 2014-02-16
    • 2021-12-16
    • 2015-04-12
    • 1970-01-01
    相关资源
    最近更新 更多