如何mod_rewrite将php-file-url重定向到新路径

时间:2014-05-19 08:43:33

标签: apache mod-rewrite

我想重定向

http://api.domain.com/api.php?debug=true
http://api.domain.com/getData/?debug=true

怎么了?

RewriteCond %{REQUEST_URI} !^/api\.php/.*
RewriteRule ^(.*)$  /getData/$1

也无法正常工作

RewriteRule ^/api\.php(.*)$ /getData/$1 [PT]

2 个答案:

答案 0 :(得分:0)

这就完成了工作

RewriteRule ^api.php$ /getData/

答案 1 :(得分:0)

首先让我告诉你两次尝试有什么问题:

RewriteCond %{REQUEST_URI} !^/api\.php/.*
RewriteRule ^(.*)$  /getData/$1

这转换为:如果网址的URI部分(在域之后和查询字符串之前)匹配api.php,请将^(。*)$转换为/ getData / $ 1 。但是,当uri与该字符串匹配时,您希望这样做,使条件过时。

RewriteRule ^/api\.php(.*)$ /getData/$1 [PT]

您尝试将此处的查询字符串与(.*)匹配,但这不是它的工作原理。除此之外,在每个目录上下文(这是.htaccess是什么)中,URL 从不以斜杠开头。 ^/因此永远不会匹配。


如果未在重写的部分中定义查询字符串,则旧URL的查询字符串将附加到新的URL。正确的重写是:

RewriteRule ^api\.php$ getData [R=301,L]

请注意,\.表示“文字点”。如果我不想逃避它,那么apisphp也会重定向。 R=301标志将使其成为外部永久重定向。 L标志会说这是通过.htaccess匹配的最后一条规则。这是为了防止其他规则在完整的URL上匹配,从而导致各种奇怪的行为。