【问题标题】:.htaccess 301 redirect to the same page with no query string.htaccess 301 重定向到没有查询字符串的同一页面
【发布时间】:2012-08-15 22:09:58
【问题描述】:

我想创建一个条件,如果页面在 URL 中有参数,例如 ?print=1 - 将此页面重定向到自身而不使用任何查询字符串。

例子:

我想要这个页面:

http://sos-med.com/en/specific_page.html?print=1&ok=1 

tp 重定向 (301) 到没有查询字符串的相同 URL:

http://sos-med.com/en/specific_page.html

我的代码是:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^*print=*$ [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}?

我没有测试服务器,请问我的代码是否正常?

上面的代码适用于网站上的每个页面。在实施该规则之前,我想尝试一个特定页面的重定向(参见我的示例)。
如何修改代码以仅使用“specific_page.html”?

我只想要 .htaccess 解决方案,而不想要 PHP 代码。

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    您已经接近了,您的 %{QUERY_STRING} 正则表达式不正确,并且您缺少 301 重定向标志:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^print=.*$ [NC]
    RewriteRule ^(.*)$ %{REQUEST_URI}? [L,R=301]
    

    试试看。


    谢谢,如果我想重定向单个特定页面:sos-med.com/en/aaa.html?print=1&ok=1 到 sos-med.com/en/aaa.html? ——

    然后您将更改规则匹配的内容:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^print=.*$ [NC]
    RewriteRule ^en/aaa.html$ %{REQUEST_URI}? [L,R=301]
    

    【讨论】:

    【解决方案2】:

    试试这个:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^(.*&)?print= [NC]
    RewriteRule ^(.*)$ /$1? [L,R=301]
    

    【讨论】:

      猜你喜欢
      • 2013-08-06
      • 1970-01-01
      • 2012-09-01
      • 2017-04-13
      • 2016-02-11
      • 2015-05-27
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多