【发布时间】:2011-06-17 19:18:34
【问题描述】:
如何重定向以下网址 http://test.com/changefile.php?filename=top-ranked-colleges
到
http://test.com/top-ranked-colleges.php 使用 htaccess 重定向。
请任何人帮助我。提前致谢。
【问题讨论】:
标签: php .htaccess mod-rewrite
如何重定向以下网址 http://test.com/changefile.php?filename=top-ranked-colleges
到
http://test.com/top-ranked-colleges.php 使用 htaccess 重定向。
请任何人帮助我。提前致谢。
【问题讨论】:
标签: php .htaccess mod-rewrite
试试这个
RewriteCond %{QUERY_STRING} ^filename=(.*)$
RewriteRule .* http://test.com/%1.php? [R=301,L]
将http://test.com/changefile.php?filename=top-ranked-colleges 更改为http://test.com/top-ranked-colleges.php
RewriteCond %{REQUEST_URI} ^/(.*).php$
RewriteRule .* http://test.com/changefile.php?filename=$1 [L]
将http://test.com/top-ranked-colleges.php 更改为http://test.com/changefile.php?filename=top-ranked-colleges
【讨论】:
这确实工作:
RewriteCond %{REQUEST_URI} ^/changefile\.php$
RewriteCond %{QUERY_STRING} ^filename=([0-9a-z_-]+)$
RewriteRule ^(.*)$ http://website.com/%1.php? [R=301,L]
%1 来自:RewriteCond %{QUERY_STRING} ^filename=([0-9a-z_-]+)$
在 RewriteRule 上有一个? 会阻止将原始查询字符串添加到重定向中
如果被调用的文件是 changefile.php,并且其中有一个带有 filename= 的查询字符串,则使用 %1 作为要转到的页面进行重定向。
如果你使用 RewriteBase,你也需要添加这个:
例如:
RewriteBase /test/
RewriteCond %{REQUEST_URI} ^/test/changefile\.php$
这不起作用:
RewriteRule ^changefile.php?filename=([a-z0-9-]+)$ http://test.com/$1.php? [R=301,L]
这是因为您无法使用 RewriteRule 或 RedirectMatch 检查查询字符串
【讨论】: