用户ID动态页面.htaccess

时间:2013-09-28 13:13:58

标签: php apache .htaccess

我有这个网址 www.mydomain.com/profile.php?id=524170354d5a6
我想把它转换成这样一个友好的网址:
www.mydomain.com/profile/524170354d5a6
我已经尝试过.htacces文件,但它不起作用,我的.htaccess位于我的文件夹proyect(www / project-> .htacces)这个位置好吗?或者它必须在(www / .htaccess) 我的.htaccess包含代码:

RewriteEngine On
RewriteRule ^profile/(\d+)*$ /profile.php?id=$1

我在互联网上看到了几个例子,但都没有用,也可能是因为我的id包含数字和字母?或者我的正则表达式错了?

2 个答案:

答案 0 :(得分:0)

您的.htaccess代码必须位于DOCUMENT_ROOT/.htaccess。还要稍微修改一下代码:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^profile/([0-9]+)/?$ /profile.php?id=$1 [L,QSA,NC]

答案 1 :(得分:0)

为了制作花哨的网址,你需要一个规则来将“丑陋的”网址变成“花哨的”网址。这是通过所谓的外部重定向完成的。您需要一个其他规则来将“花式”网址内部转换为“丑陋”网址,以便服务器知道该怎么做。如果你不通过END标志(可从Apache 2.3.9及更高版本获得)或THE_REQUEST技巧来阻止这种情况,这将创建一个永久循环。

#Rewrite fancy url to ugly url internally and stop rewriting completely
#RewriteRule ^profile/(.*)$ profile.php?id=$1 [END]

#Internal rewrite pre-2.3.9
RewriteCond %{THE_REQUEST} ^GET\ /profile/(.*)\ .*$
RewriteRule ^ profile.php?id=%1 [L]

#Redirect the ugly url to the fancy url, so that the user will always see the fancy url in the address bar
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^profile\.php$ profile/%1 [R,L]

(我不能在这台电脑上测试这个)