【问题标题】:301 Redirects working for hidden index.php but not for <file>.php301 重定向适用于隐藏的 index.php,但不适用于 <file>.php
【发布时间】:2012-10-10 10:40:57
【问题描述】:

我正在将客户从定制开发的 PHP 站点转移到 Wordpress。我现在正在使用 .htaccess 文件中的 301 重定向将旧位置移动到 Wordpress 中的新位置。其中一些正在工作,而另一些则没有(这意味着它提供了“旧”位置)。我在文件中有标准的“漂亮的永久链接”mod_rewrite 部分,然后是所有 301。

因此,具有像目录一样列出的原始位置(重写的 index.php)的那些可以工作,而具有明确命名的 php 文件的那些则不能。例子:

这行得通:

Redirect 301 /about/ http://deanchiropractic.com/about-us/

这样做(仍然为 dr-jon.php 服务):

Redirect 301 /about/dr-jon.php http://deanchiropractic.com/about-us/dr-jon-dean/

我尝试过更改顺序,尝试在 mod-rewrite 部分之前和之后使用 301,尝试使用和不使用 DirectoryIndex 行。我还验证了我正在编辑网络服务器根目录中的 .htaccess。

我宁愿让我的 .htaccess 工作,也不愿让 php 在所有未重定向的文件中执行 301 重定向(我已经尝试过,并验证了使用一个文件)。

以下是全文供参考:

DirectoryIndex index.php index.html
Redirect 301 /about/dr-dean.php http://deanchiropractic.com/about-us/christophe-dean-dc/
Redirect 301 /about/dr-jon.php http://deanchiropractic.com/about-us/dr-jon-dean/
Redirect 301 /techniques/activator.php http://deanchiropractic.com/chiropractic-techniques/activator-method/
Redirect 301 /techniques/cox-technique.php http://deanchiropractic.com/chiropractic-techniques/cox-technique/
Redirect 301 /techniques/active-release.php http://deanchiropractic.com/chiropractic-techniques/active-release-technique/
Redirect 301 /community/dinner-talk.php http://deanchiropractic.com/get-healthy/
Redirect 301 /community/refer.php http://deanchiropractic.com/get-healthy/
Redirect 301 /resources/forms.php http://deanchiropractic.com/forms/
Redirect 301 /resources/faq.php http://deanchiropractic.com/faq/
Redirect 301 /resources/articles/low-back-pain-relief.php http://deanchiropractic.com/low-back-pain-relief/
Redirect 301 /resources/articles/about-the-activator-method.php http://deanchiropractic.com/achieve-wellness-with-the-activator-method/
Redirect 301 /resources/articles/spinal-disc-treatment.php http://deanchiropractic.com/spinal-disc-treatment/
Redirect 301 /resources/articles/headache-relief-benefits.php http://deanchiropractic.com/headache-relief/
Redirect 301 /resources/articles/carpal-tunnel-relief.php http://deanchiropractic.com/carpal-tunnel-relief/
Redirect 301 /resources/articles/neck-pain-relief.php http://deanchiropractic.com/neck-pain-relief/
Redirect 301 /testimonials.php http://deanchiropractic.com/reviews/
Redirect 301 /contact.php http://deanchiropractic.com/contact-us/
Redirect 301 /about/    http://deanchiropractic.com/about-us/
Redirect 301 /techniques/   http://deanchiropractic.com/chiropractic-techniques/
Redirect 301 /community/    http://deanchiropractic.com/get-healthy/
Redirect 301 /resources/    http://deanchiropractic.com/forms/
Redirect 301 /resources/articles/   http://deanchiropractic.com/category/articles/
# 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

我已经在这个网站上进行了广泛的搜索,但找不到与我的确切问题匹配的内容……尽管我在此过程中学到了很多关于 .htaccess 的知识(仍然是 n00b tho)。谢谢....

【问题讨论】:

    标签: wordpress .htaccess http-status-code-301


    【解决方案1】:

    mod_alias 和 mod_rewrite 可能会相互干扰,因为它们都在处理管道中应用于相同的 URL。在这种情况下,可能是 URI 已更改,并且 mod_alias 标记了重定向(通过 Redirect 指令),但随后 mod_rewrite 执行其操作并重写 URI 以通过 index.php 进行路由,但 URI 被标记对于重定向,因此在管道的末尾,您有一个作为重定向发送的损坏的 URI。您可以通过只使用 mod_rewrite 并使用 L 标志来避免这种情况,这样就不会应用 wordpress 规则:

    RewriteEngine On
    RewriteRule ^/?about/dr-dean.php http://deanchiropractic.com/about-us/christophe-dean-dc/ [L,R=301]
    RewriteRule ^/?about/dr-jon.php http://deanchiropractic.com/about-us/dr-jon-dean/ [L,R=301]
    RewriteRule ^/?techniques/activator.php http://deanchiropractic.com/chiropractic-techniques/activator-method/ [L,R=301]
    

    等等……

    【讨论】:

    • 乔恩,做到了!我认为它是这样的,但对重写的东西没有足够的了解甚至开始玩弄,更不用说以合理的方式理论化我的“冲突”是什么了。非常感谢,让他们现在都工作了。
    猜你喜欢
    • 2010-12-08
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    相关资源
    最近更新 更多