2014-10-20 85 views
1

我有一个偷偷摸摸的嫌疑,这是不可能的,但想通了,我会问无论。Apache htaccess重写(漂亮的URL)

是它在所有可能采取传递给服务器的URL的形式:

http://domain.com/index.php?Action=Controller/Action&one=1&two=2&three=3 

和重写它显示为:

http://domain.com/Controller/Action/1/2/3 

我试图清理一个临界古代项目,以支持“漂亮的URL”,我真的希望使URL显示更好一点。我知道我可以设置301头重定向到新的URL,但我宁愿尽可能避免这种开销。

有什么想法?

谢谢!

回答

1

要获得

http://domain.com/index.php?Action=Controller/Action&one=1&two=2&three=3 

显示为

http://domain.com/Controller/Action/1/2/3 

您将需要使用%{QUERY_STRING}捕捉查询字符串数据。您的.htaccess文件将如下所示:

RewriteEngine On 

RewriteCond %{QUERY_STRING} ^Action=Controller/Action&one=(\d+)&two=(\d+)&three=(\d+) 
RewriteRule ^.+ /Controller/Action/%1/%2/%3 [R=301,L] 

这将设置永久重定向到新页面。你可以在这里玩耍并测试.htaccess重写规则:htaccess.madewithlove.be

0

您可以使用此代码在您的DOCUMENT_ROOT/.htaccess文件:

RewriteEngine On 
RewriteBase/

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?Action=$1/$2&one=$3&two=$4&three=$5 [L,QSA] 
+0

有趣的我将不得不放弃这个镜头。作为一个方面说明,额外的GET参数只是一个例子,是否有任何方法来过滤其中的任何数量,或者可选地将它们保持原样? EG: 动作=控制器/动作和一个= 1&2 = 2&3 = 3 打开成: 控制器/动作和一个= 1&2 = 2&3 = 3 谢谢! – 2014-10-20 22:24:25