具有多个变量的URL上的特定mod重写

时间:2013-10-27 21:45:54

标签: php .htaccess mod-rewrite url-rewriting

提前感谢帮助。我正在使用mod重写,需要帮助将特定的PHP URL转换为更漂亮的URL。

具体来说,我想转换PHP URL:

www.sample.com/subcategory.php?subcategory=sports-achievement&page=1

www.sample.com/sports/sports-achievement.html

为用户。

具体而言,规则是:

  • a)破折号之前的子类别的第一部分应该是 目录(例如,sports- - into / sports /
  • b)整个子类别为 页面字符串“sports-achievement.html
  • c)PHP页面仍然需要 传递页面字符串(因为有多个页面)

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

要获得重定向和内部重写,您必须执行以下操作。

#Redirect
RewriteCond %{THE_REQUEST} (GET|POST)\ /subcategory\.php\?subcategory=([^-]+)-([^&]+)(&(.*)|)\ HTTP
RewriteRule ^subcategory\.php$ /%2/%2-%3.html?%5 [R,L]

#Internal rewrite
RewriteRule ^(.*)/\1-(.*)\.html$ subcategory.php?subcategory=$1-$2 [QSA,L]

没有真正隐藏页面名称的方法。您可以使用后期请求并将其隐藏在帖子中。我的回答to this question显示了如何使用Cookie,但这很可能会混淆搜索引擎,因为不同的页面在同一页面名称下可用。