【问题标题】:How to re-write the link, with ".htaccess", with 2 parameters?如何用“.htaccess”用2个参数重写链接?
【发布时间】:2016-08-26 00:52:04
【问题描述】:

我有这个网站,结构如下:http://example.com/dir/index.php?first=12&second=16。现在,我想将此链接编辑为:http://example.com/dir/12/16


所以,我在http://example.com/dir/ 中添加了一个.htaccess;有内容:

RewriteEngine On
RewriteRule ^([0-9]+)$ ?first=(\d+)&second=(\d+)

我用这个简单的PHP做检查,放在http://example.com/dir/:

<?php

/* ... */

echo $_GET['first'] . ' - ' . $_GET['second'];

/* ... */

?>

当我转到http://example.com/dir/1/2 时,它返回:404。所以,我的.htaccess 无法正常工作。


我的.htaccess 有什么问题?

【问题讨论】:

  • 您只有 1 个捕获组,您的路径是 search.php?month=12&amp;day=16,而不是所有数字。
  • 您只有 1 个捕获组,那么您如何获得 2 个变量?
  • 也许是search\.php\?month=(\d+)&amp;day=(\d+)。你的错误信息是什么?
  • 这不是错误消息或对所发生情况的描述。
  • 哦,我以为你正在加载 http://example.com/dir/index.php?first=12&amp;second=16,虽然现在想起来这没有意义.. 现在得到了下面的答案,虽然看起来它可以工作。

标签: php apache .htaccess url-rewriting


【解决方案1】:

您需要为 RewriteRule 模式使用 2 个捕获组,否则将没有第二个变量,即 $2

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^dir/([0-9]+)/([0-9]+)/?$ /dir/index.php?month=$1&day=$2 [L]

【讨论】:

  • 好吧,您可以接受我的回答,因为它可以帮助您解决问题。
【解决方案2】:

将此.htaccess 放入http://example.com/dir/

RewriteEngine On
RewriteRule ^([0-9]+)/([0-9]+)/?$ ?first=$1&second=$2

很简单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 2016-05-20
    • 2014-03-11
    相关资源
    最近更新 更多