问题
I've had a hard timing learning rewrite rules in .htaccess files, so I was hoping someone could show me how to rewrite this:
test.com/home/feed/toak/nyheter/vg
into this:
test.com/index.php?r=home&f=feed;toak;nyheter;vg
This is a dynamic URL, and can have multiple elements seperated by ; in the end. I want my users to be able to type in as human friendly URLs as possible. I'm hoping someone can help!
回答1:
If you have variable number of parameters, you can't do it only with .htaccess. You could rewrite in php:
RewriteEngine On
RewriteRule .* rewrite.php [NE,L]
And parse $_SERVER['REQUEST_URI']
in rewrite.php.
For static number of 4 variables:
RewriteEngine On
RewriteRule ^(\w+)/(\w+)/(\w+)/(\w+)/(\w+) index.php?r=$1&f=$2;$3;$4;$5
Or you could do (for any number of variables):
RewriteEngine On
RewriteRule ^(\w+)/(.*) index.php?r=$1&f=$2
And use $_GET['f'] = strtr($_GET['f'], '/', ';');
in index.php.
来源:https://stackoverflow.com/questions/3077451/htaccess-file-dynamic-redirect