apache - RewriteRule 将 url 部分作为参数传递给 php 脚本

标签 apache .htaccess mod-rewrite url-rewriting

我有一个网址

 http://localhost/xxxx/cat/History?randomArg=2

我想运行

http://localhost/xxxx/cat/edit.php?name=History&randomArg=2

 http://localhost/xxxx/cat.php?name=History&randomArg=2          

(POST EDIT#2 Correction I did originally ask for edit.php but I meant cat.php)

我有一个可在我的实时服务器上运行的 RewriteRule

RewriteRule "^(.*)xxxx.*/cat/(.*)?(.*)$" $1/xxxx/cat.php?name=$2&t=123$3[QSA]

当我通过 htaccess 测试器运行测试时 https://htaccess.madewithlove.com?share=f9987308-2570-4fbe-a769-4e5031a96578 ,我明白了...

RewriteRule "^(.*)xxxx.*/cat/(.*)?(.*)$" $1/xxxx/cat.php?name=$2&t=123$3[QSA]

... enter image description here

首先,我不明白为什么 $3(例如“randomArg=2”)没有出现在 https://htaccess.madewithlove.com 上。测试站点。

其次,我已将其插入到我的 WAMP 环境中,虽然我看到 cat.php 正在运行,但我没有看到 RewriteRule 正在工作**

我的 cat.php 代码如下:

echo "<LI>_GET[*]:<PRE>" . print_r($_GET, true) . "</PRE>";
echo "<LI>_SERVER[SCRIPT_NAME]:<PRE>" . $_SERVER['SCRIPT_NAME'] . "</PRE>";
echo "<LI>_SERVER[REQUEST_URI]:<PRE>" . $_SERVER['REQUEST_URI'] . "</PRE>";

if (isset($_GET['name'])) {
    echo "<LI>GET[name]=<PRE>" . $_GET['name'] . "</PRE>";
    $params = explode("/", $_GET['name']);
    $site = array_shift($params);
    echo "<LI>shifted:[$site]";
}
else echo "<LI>No GET[name]";

if (isset($_GET['t'])) echo "<LI>t:<PRE>" . $_GET['t'] . "</PRE>";
else echo "<LI>No GET[t]";

http://localhost/xxxx/cat/History?randomArg=2 的输出为:

_GET[*]:Array
(
    [randomArg] => 2
)
_SERVER[SCRIPT_NAME]:/xxxx/cat.php
_SERVER[REQUEST_URI]:/xxxx/cat/History?randomArg=2
No GET[name]
No GET[t]

** 但如果规则不起作用,那么为什么 cat.php 会运行(因为 URL 要求“cat/History”?

(Windows、Apache 2.4.41、PHP5.4)

      作为旁注/测试,使用类似的规则(但使用 sss.xxx.com/testHtaccess/History)将其放入我的 LAMP(Apache 2.4.6)环境中,规则如下...
      RewriteRule "^(.*)testHtaccess/(.*)?(.*)$" $1/testHtaccess.php?name=$2&t=123$3[QSA]
      

      ...确实部分有效(它传递了“name”,但仍然没有 $3)!

那么我怎样才能让我的本地主机规则发挥作用呢?

** 附加(编辑后#1):

就其值(value)而言,正如 anubhava(如下)所指出的那样,我注意到我有以下 httpd-vhosts.conf 默认设置:

 # Virtual Hosts
 #
 <VirtualHost *:80>
   ServerName localhost
   ServerAlias localhost
   DocumentRoot "${INSTALL_DIR}/www"
   <Directory "${INSTALL_DIR}/www/">
     Options +Indexes +Includes +FollowSymLinks +MultiViews
     AllowOverride All
     Require local
   </Directory>
 </VirtualHost>

+MultiViews will explain "cat" turning into "cat.php"

最佳答案

  • 您无法使用 RewriteRule 匹配查询字符串(尽管您不需要在此处匹配)。
  • 您似乎已开启 MultiViews(内容协商服务)。

您可以在站点根目录 .htaccess 中使用此代码:

Options -MultiViews
RewriteEngine On

RewriteCond ^(.*/cat)/([\w-]+)/?$ [NC]
RewriteRule ^ %1.php?name=%2 [L,QSA,NC]

使用QSA,您的原始查询字符串将自动附加到新目标。

关于apache - RewriteRule 将 url 部分作为参数传递给 php 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74510406/

相关文章:

php - ELB 后面的 Symfony2 重定向到 http 而不是 https

php .htaccess url重写不正确的参数

apache - 使用 .htaccess 限制对文件的访问

apache - htaccess : add path parameter (country code) to URL & serve index. html

php - 有没有办法只允许访问以前在 .htaccess 中列出的文件?

PHP include 包含在 include 中(不同的目录)

regex - 如何使用 htaccess 重写从 url 中删除此目录/文件夹?

.htaccess - 301 将整个网站重定向到单个页面

apache - 重写旧的子文件夹 url

php - 具有单个 CloudFront 分发的多个网站不会被路由