Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php htaccess使用不同的参数名称和(或)值重定向_Php_Regex_.htaccess - Fatal编程技术网

Php htaccess使用不同的参数名称和(或)值重定向

Php htaccess使用不同的参数名称和(或)值重定向,php,regex,.htaccess,Php,Regex,.htaccess,我有一个带有多个参数的页面。是否可以使用htaccess或php更改url,如下所示 http://example.com/result.php?year=2012 Change To http://example.com/year-2012 http://example.com/result.php?category=naturals Change To http://example.com/category-naturals http://example.com/result.php?y

我有一个带有多个参数的页面。是否可以使用htaccess或php更改url,如下所示

http://example.com/result.php?year=2012
Change To
http://example.com/year-2012

http://example.com/result.php?category=naturals
Change To
http://example.com/category-naturals

http://example.com/result.php?year=2012&category=naturals
Change To
http://example.com/year-2012/category-naturals

您可以使用.htaccess来完成。试着这样做:

RewriteRule ^year-(.*)  result.php?year=$1 [PT]
RewriteRule ^category-(.*)  result.php?category=$1 [PT]
RewriteRule ^year-(.*)/category-(.*)  result.php?year=$1&category=$2 [PT]
尝试此操作。替换为
$1$2-$3
。请参阅演示


您可以在根目录中设置这些规则。htaccess:

RewriteRule ^year-([^/]+)/?$                  result.php?year=$1             [NC,L,QSA]
RewriteRule ^category-([^/]+)/?$              result.php?category=$1         [NC,L,QSA]
RewriteRule ^year-([^/]+)/category-([^/]+)/?$ result.php?year=$1&category=$2 [NC,L,QSA]

单参数的解决方案是完美的。但是,两个参数的解决方案并不完美。它作为单个参数,当我打印请求值时,我会得到以下结果。数组([year]=>2012/category-2013)我必须学习正则表达式
RewriteRule ^year-([^/]+)/?$                  result.php?year=$1             [NC,L,QSA]
RewriteRule ^category-([^/]+)/?$              result.php?category=$1         [NC,L,QSA]
RewriteRule ^year-([^/]+)/category-([^/]+)/?$ result.php?year=$1&category=$2 [NC,L,QSA]