【问题标题】:Mod Rewrite lost all GET parameters [PHP]Mod Rewrite 丢失了所有 GET 参数 [PHP]
【发布时间】:2020-11-19 10:15:24
【问题描述】:

今天我开始为 php 脚本重写我的 mod。 关于设置的一些话

我使用子域,例如test.testsystem.com 指向 root/test/ 所以我的 PHP 脚本有多个 GET 参数,如下所示:

1) https://test.testsystem.com/index.php?a=foo
2) https://test.testsystem.com/index.php?a=foo&v=bar
3) https://test.testsystem.com/index.php?a=foo&c=testcode

所以有了 mod rewrite 我想拥有

1) https://test.testsystem.com/foo
2) https://test.testsystem.com/foo/bar
3) https://test.testsystem.com/foo/c/testcode

我现在拥有的是

Options -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /test/
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?a=$1 [QSA]
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?a=$1&v=$2 [QSA]

对于前两个 URL。现在它不再出现错误 500 但问题是我的 php 脚本中没有任何 GET 参数。

也许有人可以在这里为我提供一个可行的解决方案。

问候

【问题讨论】:

  • 您的规则都要求请求的 URL 路径以两个字母开头,后跟一个斜杠。您提到的三个输入 URL 示例均不满足该条件。
  • 并且尝试在两个连续的规则中匹配 exact same 模式也没有什么意义。
  • 是的,你是对的,我将其更改为 RewriteRule ^(.*[^/])$ index.php?a=$1 [QSA] 作为第一条规则,但它也不起作用
  • 它是否适用于您的第一个 URI RewriteRule ^foo$ index.php?a=foo [L] 在地址栏中输入 /foo 只是为了查看您的 php 脚本中是否有 foo 变量。
  • 我用test.testsystem.com/foo 对其进行了测试,并在index.php 中使用了以下内容:$request = array_merge($_GET, $_POST); print_r($request); 我得到的只是array()....所以数组中没有条目

标签: php apache .htaccess mod-rewrite


【解决方案1】:

假设您的 test.domain.com 指向 /test 文件夹,您可以在 /test/.htaccess 中使用类似以下内容。

Options -Multiviews
RewriteEngine on
#1)Rewrite /foo to /index.php?a=foo
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?a=$1  [L]
#2)rewrite /foo/bar to /index.php?a=foo&v=bar
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?a=$1&v=$2  [L]
#3)rewrite /foo/bar/buzz to /index.php?a=foo&v=bar&z=buzz
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?a=$1&v=$2&z=$3  [L]

RewriteConds 在每个规则中都很重要,以避免重写现有文件和目录。

【讨论】:

    猜你喜欢
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多