2012-08-15 88 views
0

我想创建一个条件,如果页面中有参数,如?print = 1 - 将此页面本身重定向到没有任何查询字符串。.htaccess 301重定向到没有查询字符串的同一页面

例子:

我想这个网页:

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代码。

回答

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] 
+0

谢谢,如果我想重定向一个特定的页面: HTTP ://sos-med.com/en/aaa.html?print = 1&ok = 1 to http://sos-med.com/en/aaa.html? – 2012-08-15 22:46:55

+0

@VitalyShalman我附加了答案 – 2012-08-15 22:52:08

0

试试这个来代替:

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