0

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

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

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

4

2 回答 2

0

您的.htaccess代码需要在DOCUMENT_ROOT/.htaccess. 还要稍微修改您的代码:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^profile/([0-9]+)/?$ /profile.php?id=$1 [L,QSA,NC]
于 2013-09-28T13:23:20.513 回答
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]

(我无法在这台电脑上测试)

于 2013-09-28T13:24:03.023 回答