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

时间:2014-10-09 13:04:37

标签: 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?year=2012&category=naturals
Change To
http://example.com/year-2012/category-naturals

3 个答案:

答案 0 :(得分:1)

您可以使用.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 :(得分:0)

(http:\/\/.*\/).*?\?(?:(\w+)=(\w+)&?)*

试试这个。$1$2-$3。见。演示。

http://regex101.com/r/yA1jY6/2

答案 2 :(得分:0)

您可以在根目录中使用这些规则.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]