简体   繁体   English

使用GET变量创建漂亮的url

[英]Create pretty url's with GET variables

At this moment I have a situation in which pretty url's are rewritten so that everything goes by the index.php, while maintaining the $_GET variables. 目前,我处于一种情况,其中漂亮的url被重写,以便一切都由index.php进行,同时保持$_GET变量。 The htaccess looks as follows: htaccess如下所示:

RewriteEngine On
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?p=$1&projectid=$2&sub=$3&type=$4
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?p=$1&projectid=$2&sub=$3
RewriteRule ^([^/\.]+)/([^/\.]+)?$ index.php?p=$1&projectid=$2
RewriteRule ^([^/\.]+)/?$ index.php?p=$1

This works fine. 这很好。 However, I want it to work the other way around as well. 但是,我希望它也能以其他方式工作。 If someone goes to http://www.example.com/index.php?p=page&projectid=123 that they will be redirected to http://www.example.com/page/123 . 如果有人访问http://www.example.com/index.php?p=page&projectid=123 ,他们将被重定向到http://www.example.com/page/123

Is this possible, and if so, how? 这可能吗?如果可以,怎么办? All I've managed so far is creating a redirect loop. 到目前为止,我所要做的就是创建一个重定向循环。

For those interested, the redirect loop I created looks as follows: 对于那些感兴趣的人,我创建的重定向循环如下所示:

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

I want it to work the other way around as well. 我也希望它能以其他方式工作。 If someone goes to http://www.example.com/index.php?p=page&projectid=123 that they will be redirected to http://www.example.com/page/123 如果有人访问http://www.example.com/index.php?p=page&projectid=123 ,他们将被重定向到http://www.example.com/page/123

Yes it indeed possible but you will need to use THE_REQUEST variable for that. 是的,确实有可能,但是您需要为此使用THE_REQUEST变量。

RewriteCond %{THE_REQUEST} \s/+index\.php\?p=([^\s&]+)&projectid=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]

You can build similar rule for other URLs as well. 您也可以为其他URL建立类似的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM