创建漂亮的URL

时间:2013-05-21 02:33:41

标签: .htaccess

我正在尝试为我丑陋的网址制作友好的网址。

mod目录包含index.phpuser.php文件。 index.php只有一些链接,具有各种格式的用户页面。

mod目录的路径,如下所示:http://localhost/mod/

此时用户的网址是这样的......

http://localhost/mod/user.php?id=Ricky etc..

我需要让那个丑陋的人看起来很漂亮......

http://localhost/mod/user/Ricky

我在我的htaccess文件中尝试过,到目前为止这是该文件中的代码。

# Enable Rewriting
RewriteEngine on

# Rewrite user URLs
#   Input:  user/NAME/
#   Output: user.php?id=NAME
RewriteRule ^user/([a-z]+)/?$ user.php?id=$1 

这对我不起作用。希望有人能帮助我。 谢谢。

2 个答案:

答案 0 :(得分:1)

RewriteRule ^user/([A-Za-z]+)/?$ user.php?id=$1 [NC,L]

状态结束行和不区分大小写[NC,L]

您也可以使用\ w匹配所有文本字符(非数字)

RewriteRule ^user/(\w+)/?$ user.php?id=$1 [NC,L]

这是正则表达式的备忘单: http://www.regular-expressions.info/reference.html

答案 1 :(得分:0)

RewriteRule ^mod/user.php?id=$ mod/user/([a-zA-Z])/$1 
相关问题