在.htaccess中使用mod_rewrite重写URL
我正在尝试使用mod_rewrite apache模块重写URL。 我尝试使用它,如下所示:在.htaccess中使用mod_rewrite重写URL,.htaccess,mod-rewrite,url-rewriting,.htaccess,Mod Rewrite,Url Rewriting,我正在尝试使用mod_rewrite apache模块重写URL。 我尝试使用它,如下所示: Options +FollowSymlinks RewriteEngine on RewriteBase / RewriteRule ^wants/testing.htm wants.php?wantid=$1 [L] 我在访问文件的目录中创建了一个.htaccess文件 mod_重写也已启用。 做了这么多,我还是没能让它工作 有人能帮我吗?如果我遗漏了什么,请告诉我 提前感谢,, Gnanesh我认
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm wants.php?wantid=$1 [L]
我在访问文件的目录中创建了一个.htaccess文件
mod_重写也已启用。
做了这么多,我还是没能让它工作
有人能帮我吗?如果我遗漏了什么,请告诉我
提前感谢,,
Gnanesh我认为前导斜杠(或者说缺少前导斜杠)可能是你的问题:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm /wants.php?wantid=$1 [L]
否则Apache可能会在/wants/wants.PHP中查找PHP文件,因为它会将其视为相对URL。根据OP的评论:
URL显示
mydomain.com/wants.php?wantid=123。我
需要让它看起来像
mydomain.com/wants/123
这应该适用于您的情况:
RewriteEngine on
RewriteBase /
RewriteRule ^wants/([0-9]+)$ /wants.php?wantid=$1 [L]
它将允许您使用,并将悄悄地将其重写为wants.php?wantid=123嗯,因此我对它进行了一些思考,我想您可以这样做(如果我理解正确,只需根据您的评论更改regexp): 但是我想您也可以省去$1引用,仍然可以访问
wants.php
中的id。所以
RewriteRule ^wants/\d+$ /wants.php [L]
也应该有效,然后你可以使用
<?php
$request = split('/', $_SERVER["REQUEST_URI"])[1];
?>
在
wants.php
中,数组的最后一个元素将是您的id(或您决定重写并发送到脚本的任何其他内容)。如果您想使用wants目录中.htaccess文件中的规则,您必须从模式的开始处删除上下文wants/
。所以只要:
RewriteEngine on
RewriteRule ^([0-9]+)$ /wants.php?wantid=$1 [L]
否则,如果要使用根目录中.htaccess文件中的规则:
RewriteEngine on
RewriteRule ^wants/([0-9]+)$ wants.php?wantid=$1 [L]
到底什么对你不起作用?您希望如何重写URL以及从何处提取wantid参数?URL显示mydomain.com/wants.php?wantid=123。我需要让它看起来像mydomain.com/wants/123$1是对URL中的$\u GET参数wantid的引用。如果我不够清楚,请告诉我。我不熟悉URL rewrite^/wants/testing.htm/wants.php?wantid=$1[L]@SpliFF no,这是用于.conf文件而不是.htaccess的
RewriteEngine on
RewriteRule ^wants/([0-9]+)$ wants.php?wantid=$1 [L]