htaccess rewrite rule
I have following url
http://domain/phpfile.php/something1-param1/something2-param2/somethin3-param3
Can i rewrite it with htaccess to
http://domain/phpfile.php?something1=param1&something2=pa开发者_运维问答ra2&something3=param3
also params are dynamic they can be 1,2,3 or 4
Just use:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^phpfile.php/something1-([^/]+)/something2-([^/]+)/somethin3-([^/]+)$ phpfile.php?something1=$1&something2=$2&something3=$3 [QSA]
Then in code Use $_GET
You can use this simple solution
RewriteEngine On
RewriteRule ^phpfile.php(.*)/([^/-]+)-([^/]*)/?$ phpfile.php$1?$2=$3 [QSA]
It will work with as many params as you like.
Example:
phpfile.php/name-walter/age-30
becomes phpfile.php?name=walter&age=30
If the list of parameters is going to be variable, and you don't want to include separate rules for each case in your .htaccess
file, you might as well just catch the whole querystring and parse it internally.
You can use something like this RewriteRule ^phpfile.php/(.*)$ phpfile.php?params=$1
(untested), and then in your php file just manually parse the query string like this:
<?php
#$_GET['params'] = 'something1-param1/something2-param2/somethin3-param3';
preg_match_all('/(\w+)-(\w+)/', $_GET['params'], $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$_GET[$match[1]] = $match[2];
}
unset($_GET['params']);
var_dump($_GET);
?>
This will set up your $_GET
superglobal to contain the key => value pairs as if they had been passed as individual parameters.
精彩评论