Php htaccess使用不同的参数名称和(或)值重定向
我有一个带有多个参数的页面。是否可以使用htaccess或php更改url,如下所示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
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]