I am trying to rewrite the URL using the mod_rewrite apache module. I am trying to use it as below :
我正在尝试使用mod_rewrite apache模块重写URL。我试图使用它如下:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm wants.php?wantid=$1 [L]
I have created a .htaccess file in the directory from where the files are accessed.
我在访问文件的目录中创建了一个.htaccess文件。
The mod_rewrite is also enabled. With all these done, i still haven't been able to get it working.
mod_rewrite也已启用。完成所有这些后,我仍然无法使其正常工作。
Can someone please help me with this? Please let me know if i am missing something
有人可以帮我这个吗?如果我错过了什么,请告诉我
Thanks in Advance, Gnanesh
先谢谢Gnanesh
4 个解决方案
#1
As per OP's comment:
根据OP的评论:
The URL shows mydomain.com/wants.php?wantid=123. I need to make it look like mydomain.com/wants/123
该URL显示mydomain.com/wants.php?wantid=123。我需要让它看起来像mydomain.com/wants/123
This should work for your case:
这适用于您的情况:
RewriteEngine on
RewriteBase /
RewriteRule ^wants/([0-9]+)$ /wants.php?wantid=$1 [L]
It will allow you to use http://yoursite.com/wants/123 and will silently rewrite it to wants.php?wantid=123
它允许你使用http://yoursite.com/wants/123并将它默默地重写为wants.php?wantid = 123
#2
I think a leading slash (or rather the lack thereof) might be yoru problem:
我认为一个主要的斜线(或者说缺乏斜线)可能是yoru问题:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm /wants.php?wantid=$1 [L]
Otherwise Apache might be looking for the PHP file in /wants/wants.php as it'll be treating it as a relative URL.
否则Apache可能会在/wants/wants.php中查找PHP文件,因为它会将其视为相对URL。
#3
Hmm, so I gave it some thought and I guess you could do something like this ( only changed the regexp according to your comment, if I understood correctly ):
嗯,所以我给了它一些想法,我想你可以做这样的事情(只根据你的评论改变了正则表达式,如果我理解正确的话):
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/\d+$ /wants.php?wantid=$1 [L]
But I guess you could also leave out the $1 reference, and still be able to access the id in your wants.php
. So
但我想你也可以省去$ 1的引用,仍然可以访问你的wants.php中的id。所以
RewriteRule ^wants/\d+$ /wants.php [L]
should work too, and you can then use something like
也应该工作,然后你可以使用类似的东西
<?php
$request = split('/', $_SERVER["REQUEST_URI"])[1];
?>
in your wants.php
where the last element of the array would be your id ( or anything else you ever decide to rewrite and send to the script ).
在您的wants.php中,数组的最后一个元素将是您的id(或者您决定重写并发送到脚本的任何其他内容)。
#4
If you want to use the rule in the .htaccess file in your wants directory, you have to strip the contextual wants/
from the start of the pattern. So just:
如果要在want目录中的.htaccess文件中使用该规则,则必须从模式的开头剥离上下文所需/。所以就:
RewriteEngine on
RewriteRule ^([0-9]+)$ /wants.php?wantid=$1 [L]
Otherwise, if you want to use the rule in the .htaccess file in your root directory:
否则,如果要在根目录中的.htaccess文件中使用该规则:
RewriteEngine on
RewriteRule ^wants/([0-9]+)$ wants.php?wantid=$1 [L]
#1
As per OP's comment:
根据OP的评论:
The URL shows mydomain.com/wants.php?wantid=123. I need to make it look like mydomain.com/wants/123
该URL显示mydomain.com/wants.php?wantid=123。我需要让它看起来像mydomain.com/wants/123
This should work for your case:
这适用于您的情况:
RewriteEngine on
RewriteBase /
RewriteRule ^wants/([0-9]+)$ /wants.php?wantid=$1 [L]
It will allow you to use http://yoursite.com/wants/123 and will silently rewrite it to wants.php?wantid=123
它允许你使用http://yoursite.com/wants/123并将它默默地重写为wants.php?wantid = 123
#2
I think a leading slash (or rather the lack thereof) might be yoru problem:
我认为一个主要的斜线(或者说缺乏斜线)可能是yoru问题:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm /wants.php?wantid=$1 [L]
Otherwise Apache might be looking for the PHP file in /wants/wants.php as it'll be treating it as a relative URL.
否则Apache可能会在/wants/wants.php中查找PHP文件,因为它会将其视为相对URL。
#3
Hmm, so I gave it some thought and I guess you could do something like this ( only changed the regexp according to your comment, if I understood correctly ):
嗯,所以我给了它一些想法,我想你可以做这样的事情(只根据你的评论改变了正则表达式,如果我理解正确的话):
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/\d+$ /wants.php?wantid=$1 [L]
But I guess you could also leave out the $1 reference, and still be able to access the id in your wants.php
. So
但我想你也可以省去$ 1的引用,仍然可以访问你的wants.php中的id。所以
RewriteRule ^wants/\d+$ /wants.php [L]
should work too, and you can then use something like
也应该工作,然后你可以使用类似的东西
<?php
$request = split('/', $_SERVER["REQUEST_URI"])[1];
?>
in your wants.php
where the last element of the array would be your id ( or anything else you ever decide to rewrite and send to the script ).
在您的wants.php中,数组的最后一个元素将是您的id(或者您决定重写并发送到脚本的任何其他内容)。
#4
If you want to use the rule in the .htaccess file in your wants directory, you have to strip the contextual wants/
from the start of the pattern. So just:
如果要在want目录中的.htaccess文件中使用该规则,则必须从模式的开头剥离上下文所需/。所以就:
RewriteEngine on
RewriteRule ^([0-9]+)$ /wants.php?wantid=$1 [L]
Otherwise, if you want to use the rule in the .htaccess file in your root directory:
否则,如果要在根目录中的.htaccess文件中使用该规则:
RewriteEngine on
RewriteRule ^wants/([0-9]+)$ wants.php?wantid=$1 [L]