【问题标题】:301 Redirects Work, Rewrite Rules Used for Redirects Not Working301 重定向工作,重写用于重定向的规则不起作用
【发布时间】:2014-04-15 16:52:28
【问题描述】:

下面是我们的 .htaccess 文件。所有 301 重定向都可以正常工作。之后在文件中,我们用于重定向的中间 ReWriteRule 都不起作用。 store/locations.php 的第一个重写规则非常简单,应该可以正常工作。

我确实认识到,在那个简单的重写规则之后,所有其他重写规则都包含一个“?”这可能需要我们做一些其他的事情(即逃避“?”)。但我什至无法让重写规则在简单的 url 上工作,所以想知道我们可能做错了什么。

有趣的是,.htaccess 文件底部的重写条件和重写规则确实可以正常工作(将任何非 www url 转换为 www url)。

Redirect 301 /store/index.php http://www.pelleline.com
Redirect 301 /store/contact-us.php http://www.pelleline.com/contact-us.php
Redirect 301 /index.html http://www.pelleline.com
Redirect 301 /index.htm http://www.pelleline.com
Redirect 301 /store/index.html http://www.pelleline.com
Redirect 301 /store/index.htm http://www.pelleline.com

RewriteEngine on
RewriteRule ^store/locations.php?/?$ http://www.pelleline.com/locations.php [L,R=301]

RewriteRule ^store/product-list.php?a_testoni_shoes-pg1-cid46.html?/?$        
http://www.pelleline.com/a-testoni-shoes/cid-46-1.html [L,R=301]

RewriteRule ^store/product-list.php?alden_shoes-pg1-cid47.html?/?$     
http://www.pelleline.com/alden-shoes/cid-47-1.html [L,R=301]



RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

ErrorDocument 404 /404.php

AddDefaultCharset UTF-8

【问题讨论】:

    标签: php apache .htaccess mod-rewrite redirect


    【解决方案1】:

    您无法匹配重写规则中的查询字符串,您需要在条件中使用%{QUERY_STRING} var:

    RewriteEngine on
    RewriteCond %{QUERY_STRING} ^/?$
    RewriteRule ^store/locations.php$ http://www.pelleline.com/locations.php [L,R=301]
    
    RewriteCond %{QUERY_STRING} ^a_testoni_shoes-pg1-cid46.html?/?$
    RewriteRule ^store/product-list.php$ http://www.pelleline.com/a-testoni-shoes/cid-46-1.html [L,R=301]
    
    RewriteCond %{QUERY_STRING} ^alden_shoes-pg1-cid47.html?/?$
    RewriteRule ^store/product-list.php$ http://www.pelleline.com/alden-shoes/cid-47-1.html [L,R=301]
    

    【讨论】:

    • 感谢您的意见。这似乎行不通。它看起来确实与我尝试过的其他一些东西很接近,但它不起作用。
    • 太奇怪了,.htaccess 文件的其他部分,包括最后的重写条件(非 www 到 www)工作正常,但无论我使用什么方法,包括上面的,来重定向/重写特定网站的那些网址,它们会被忽略
    • 这实际上工作得很好,谢谢!我的问题是子文件夹中有第二个 .htaccess 文件阻止了这个伟大的代码工作。再次感谢
    猜你喜欢
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    相关资源
    最近更新 更多