2017-07-18 230 views
0

我有一个已经迁移到新平台的应用程序。任务是相似的,我想重定向GET参数到一个目录。例如Apache .htaccess将目录重写为目录

http://gallery/index.php?gal=ABC => http://photos/gal/ABC 
    and 
http://gallery/?gal=DEF   => http://photos/gal/DEF 

并且不被逮住的任何它重定向到http://photos

我已经试过

RewriteEngine on 
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$ 
RewriteRule ^(/index.php)$ /%1/%2? 

但是我得到的是一个404,没有重定向。同样,我已经试过

RedirectMatch ^/index\.php\?=gal(.*) http://photos/gal/$1 

,但我无法在原来的URL逃离?

什么是正确的方式去做这件事?

回答

2
RewriteCond %{QUERY_STRING} ^(.+)=(.+)$ 
RewriteRule ^index.php$ http://photos %1/%2? [L,NC,R=301] 
+0

这个可以清楚地看到 – vincenth520

+0

不得不修复'照片%1'之间的额外空间,但除此之外一切都很好!谢谢你的帮助! – SteppingHat

+0

不客气,请接受此答案。谢谢 – vincenth520

1

您的订单被倒过来。重写它在你的面前

RewriteRule /(.+)\/(.+) index.php?$1=$2

+0

似乎并没有为我工作。什么都没发生。我可以确认.htaccess文件正在工作,因为输入'RedirectMatch /(.+)http:// photos/$ 1'可以很好地路由'http://gallery/index.php?gal = ABC''=>'' http:// photos /?gal = ABC' – SteppingHat

+0

你试试这个'RewriteRule /(.+)\/(.+)http://gallery/index.php?$ 1 = $ 2' – vincenth520

+0

仍然没有任何内容:/ – SteppingHat