0

我是 mod_rewrite 或 Regex 的真正新手。因此,对于以下问题,我只需要您的帮助。

我有一个看起来像这样的 PHP 页面:

stuff.php?id=1&text=2

我知道想要看起来像

stuff/2.html

你们中是否有人考虑过 htaccess 的 RewriteRule 行让它看起来像这样?

提前非常感谢!

4

2 回答 2

1

此特定页面的重写规则:

RewriteRule ^stuff/2\.html$ stuff.php?id=1&text=2

如果2应该是动态的:

RewriteRule ^stuff/([0-9]+)\.html stuff.php?id=1&text=$1

一点解释:

  • ^$代表字符串的开始和结束,所以我们不匹配longstuff/2.html.php.
  • The dot has to be escaped \. because otherwise it has a special meaning in RegEx ("any character")
  • the parantheses in the second pattern are a "capture group", their content will be available in the rewrite as $n (with n = number of capture group, in this case 1)
  • [0-9] is a character class, matches one character of the class, in this case a digit
  • + means "one or more"
于 2013-03-20T13:23:55.563 回答
0

这是重定向stuff/2.html到的规则stuff.php?id=1&text=2

RewriteRule ^stuff/([\d]+)\.html$ stuff.php?id=1&text=$1 [L]

Notice[\d]+将只接受数字,如果您想允许字母和插入符号,请使用以下规则:

RewriteRule ^stuff/([\w-]+)\.html$ stuff.php?id=1&text=$1 [L]
于 2013-03-20T13:23:10.420 回答