【发布时间】:2012-03-15 12:07:12
【问题描述】:
我希望能够获取网址
www.mydomain.com/profile.php?user=THEUSERNAME
把它变成
www.mydomain.com/THEUSERNAME
几乎像 Twitter。
我也想把它放在我可以去的地方
www.mydomain.com/THEUSERNAME
它会转到用户的页面,而不会将 URL 更改回长 URL。我不知道这是如何完成的,我也不知道它是否需要 .htaccess、PHP 或两者都需要。 请帮忙。
找到解决方案
在您的 .htaccess 文件中:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ profile.php?user=$1 [L]
您还需要为所有 CSS 和 JS 文件提供 absolute URL。例如,您必须输入 src="http://yourdomain.com/css/main.css" 而不是 src="css/main.css" 。原因是如果您的 url 有反斜杠(即 yourdomain.com/USERNAME/),它会认为它在下一个目录中并且会导致冲突。 Absolute 解决了 CSS 和 JS 的问题。
在profile.php的中:
不是这个:
<link href="css/allpage.css" rel="stylesheet" type="text/css" />
这个:
<link href="http://yourdomain.com/css/allpage.css" rel="stylesheet" type="text/css" />
【问题讨论】: