Reputation: 6337
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]
Upvotes: 1
Views: 197
Reputation: 785246
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 tohttp://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.
Upvotes: 1