【问题标题】:.htaccess RewriteRule path to query string.htaccess RewriteRule 查询字符串的路径
【发布时间】:2019-01-02 12:07:31
【问题描述】:

我正在尝试实现类似的网址

example.com/path1 到 example.com/index.php?p1=path1

example.com/path1/path2 到 example.com/index.php?p1=path1&p2=path2

在我的 .htaccess 中,我有:

RewriteEngine On

RewriteRule ^(.*)/(.*)$ /index.php?c=$1&g=$2 [NC,L]

RewriteRule ^([a-zA-Z0-9-_]+)$ /index.php?g=$1 [NC,L]

它适用于 example.com/path1/path2 但适用于 example.com/path1 的情况> 仅当 url 中没有点时才有效。例如,我想将 example.com/mydomain.com 工作到 example.com/index.php?g=domain.com 但我无法使其工作。

你能帮我解决这个问题吗?

【问题讨论】:

  • ([a-zA-Z0-9-_]+) 您明确地只处理字母数字加下划线的情况。这将排除您的示例案例。

标签: .htaccess mod-rewrite


【解决方案1】:

我会这样处理:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1

然后,无论您使用什么服务器端语言,我都会使用 PHP 解析 $_GET['param']。

$params = explode('/', $_GET['params']);

【讨论】:

  • 您实际上甚至不需要在重写时通过params var,只需使用$_SERVER['REQUEST_URI'] 来获取它 - 这使得规则更加简洁RewriteRule . /index.php [L,QSA]
  • 非常感谢,它成功了,我已经这样解决了!
猜你喜欢
  • 2016-03-26
  • 2022-11-24
  • 1970-01-01
  • 2013-06-29
  • 2011-03-07
  • 1970-01-01
  • 2018-07-25
  • 2013-12-21
  • 2014-08-01
相关资源
最近更新 更多