1

现在我在 .htaccess 文件中使用这些

RewriteRule (.*)/(.*)/(.*)/(.*)\.html$ index.php?lng=$1&page=$2&subpage=$3&subpage1=$4 [L]
RewriteRule (.*)/(.*)/(.*)\.html$ index.php?lng=$1&page=$2&subpage=$3 [L]
RewriteRule (.*)/(.*)\.html$ index.php?lng=$1&page=$2 [L]
RewriteRule (.*)\.html$ index.php?page=$1 [L]
RewriteRule index\.html$ index.php [L]

并且工作正常。

如果我想添加另一个子页面,那么我必须输入一个包含所有获取的新 RewriteRule。例如,如果我想在前面的代码中添加另一个子页面,我应该输入:

 RewriteRule (.*)/(.*)/(.*)/(.*)/(.*)\.html$ index.php?lng=$1&page=$2&subpage=$3&subpage1=$4&subpage1=$5 [L]

但是我怎样才能使它更简单和通用来处理所有获取?

注意:我不关心 GET 名称(页面、子页面 1 ....)

编辑:我希望能够获取查询字符串,但我不关心名称(键):page、subpage、subpage1 等

4

1 回答 1

0

你只能使用一个(looooong)规则,但它会很丑,准备好......

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([^/]+)(/([^/]+))?(/([^/]+))?(/([^/]+))?(/([^/]+))?\.html$  index.php?p1=$1&p2=$3&p3=$5&p4=$7&p5=$9 [L]

这里的诀窍是使用模式(/([^/]+))?并获得受损参数 (1,3,5...)

例如,使用 url www.domain.com/a/b/c.html,您将获得:

  • $1 = 一个
  • $2 = /b
  • $3 = b
  • $4 = /c
  • $5 = c
  • $6~$9 = null
于 2012-06-09T23:46:40.003 回答