用?-sign重写URL

时间:2013-12-20 01:18:25

标签: apache .htaccess mod-rewrite

在.htaccess中我想重写网址......

http://example.com/folder/file.php?folder/images/image0001.jpg

...变为

http://example.com/folder/images/image0001.jpg

我一直在努力......

RewriteRule ^folder/file.php?(.*)$ http://example.com/$1 [R=301,L]

但最终却出现了不必要的情况?-sign ......

http://example.com/?folder/images/image0001.jpg

我如何摆脱?-sign?

1 个答案:

答案 0 :(得分:1)

您无法在重写规则中与?匹配,您需要在%{QUERY_STRING}%{THE_REQUEST}中进行匹配。如果你想将它重写为http://example.com/folder/images/image0001.jpg,意味着文件**实际上是/folder/images/image0001.jpg,那么你想要这个:

RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^folder/file\.php$ http://example.com/%1? [R=301,L]

如果那里没有任何东西,显然重定向只会导致404 Not Found。如果这应该是使URL看起来“漂亮”的一种方法,那么您需要将URL 重新重写为查询字符串:

RewriteCond %{THE_REQUEST} \ /+folder/file\.php\?([^&\ ]+)
RewriteRule ^folder/file\.php$ http://example.com/%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^folder/(.*)$ /folder/file.php?folder/$1 [L]