【问题标题】:.htaccess rewrite urls with many GET s, to dir style like: /page/subpage/subpage1/subpage2/ and so on.htaccess 用许多 GET 重写 url,到 dir 样式,如:/page/subpage/subpage1/subpage2/ 等等
【发布时间】:2012-06-13 03:48:42
【问题描述】:

现在我在 .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 等

【问题讨论】:

标签: html apache .htaccess url-rewriting get


【解决方案1】:

你只能使用一个 (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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 2019-07-19
    • 1970-01-01
    相关资源
    最近更新 更多