2009-05-20 60 views
0

我正在尝试使用mod_rewrite apache模块重写URL。 我想如下使用它:在.htaccess中使用mod_rewrite进行URL重写

Options +FollowSymlinks 
RewriteEngine on 
RewriteBase/
RewriteRule ^wants/testing.htm wants.php?wantid=$1 [L] 

我创建从哪里访问文件目录中的.htaccess文件。

mod_rewrite也被启用。 完成所有这些后,我仍然无法使其工作。

有人可以帮助我吗?请让我知道,如果我失去了一些东西提前

感谢, Gnanesh

+0

什么不适合你?您如何期望URL被重写以及您想从何处提取wantid参数? – duckyflip 2009-05-20 08:05:34

+0

该URL显示mydomain.com/wants.php?wantid=123。 我需要使它看起来像mydomain.com/wants/123 – gnanesh 2009-05-20 08:23:10

回答

0

我认为一个领导斜线(或者更确切地说,它的缺乏)可能是夜的问题:

Options +FollowSymlinks 
RewriteEngine on 
RewriteBase/
RewriteRule ^wants/testing.htm /wants.php?wantid=$1 [L] 

否则Apache的可能在/wants/wants.php中寻找PHP文件,因为它会将其视为相对URL。

2

按OP的评论:

的URL显示 mydomain.com/wants.php?wantid=123。我 需要使它看起来像 mydomain.com/wants/123

这应该工作你的情况:

RewriteEngine on 
RewriteBase/
RewriteRule ^wants/([0-9]+)$ /wants.php?wantid=$1 [L] 

它可以让你使用http://yoursite.com/wants/123将会默默地改写到wants.php?wantid = 123

0

嗯,所以我给了它一些想法,我想你可以做这样的事情(只改变了正则表达式acco录制您的意见,如果我理解正确的):

Options +FollowSymlinks 
RewriteEngine on 
RewriteBase/
RewriteRule ^wants/\d+$ /wants.php?wantid=$1 [L] 

但我猜你也可以离开了$ 1个参考,并仍然能够访问该ID在你wants.php。所以

RewriteRule ^wants/\d+$ /wants.php [L] 

应太,然后你就可以在你的wants.php使用类似

其中数组的最后一个元素将是您的身份证(或其他任何你决定重写和发送到脚本)。

0

如果您想在您的想要目录中使用.htaccess文件中的规则,则必须从模式的开头去除上下文wants/。所以只是:

RewriteEngine on 
RewriteRule ^([0-9]+)$ /wants.php?wantid=$1 [L] 

否则,如果你想使用规则中的。htaccess文件在你的根目录下:

RewriteEngine on 
RewriteRule ^wants/([0-9]+)$ wants.php?wantid=$1 [L]