简体   繁体   中英

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. The htaccess looks as follows:

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 .

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

Yes it indeed possible but you will need to use THE_REQUEST variable for that.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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