htaccess干净的个人资料链接

时间:2012-01-26 17:49:57

标签: .htaccess url-rewriting

目前我正在使用以下HTACCESS代码

RewriteEngine On

# Lose the www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]

# Clean profile URLs
RewriteRule ^([a-zA-Z0-9_-]+)(/?)$ profile.php?user=$1

所以当我转到http://mysite.com/username时链接确实有效,我看到了个人资料。但我仍然可以使用http://mysite.com/profile.php?user=username

我想要的是第二个被引导到http://mysite.com/username的简短版本。

另一个问题是当我浏览http://www.mysite.com/username时,它会将网址重写为http://mysite.com/profile.php?user=username

另外,如何对某些文件夹(例如我的图像文件夹)避免这种情况?如果我转到图片文件夹,我会得到http://mysite.com/images/?user=images

此外,如果干净的网址后面有斜线,页面也会很奇怪。

2 个答案:

答案 0 :(得分:0)

如果网站尚未生效,我不会担心/profile.php?user=username网址。因为人们必须知道有一个php文件名profile.php。如果您从未在html中直接创建任何链接到profile.php,没有人会知道。

如果网站已经上线,请告诉我们。

关于图像问题。

RewriteCond $1 !^images/
RewriteRule ^([a-zA-Z0-9_-]+)(/?)$ profile.php?user=$1

还要做一个css和/或js文件夹

RewriteCond $1 !^(images|css|js)/
RewriteRule ^([a-zA-Z0-9_-]+)(/?)$ profile.php?user=$1

尾部斜杠的最后一个问题很可能与你的html中使用相对url有关。尝试将href="path/resource.ext"替换为href="/path/resource.ext"src="..."等。

相同

答案 1 :(得分:0)

您需要在更改网址规则上添加“上一步”操作。我将代码更改为以下内容:

# Lose the www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]

# Exclude existing files from redirect
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.* - [L]

# Clean profile URLs
RewriteRule ^([^/]*)\/$ http://businessgame.be/$1 [R=301]
RewriteRule ^([^/]*)(/?)$ profile.php?user=$1

这似乎很有效。

相关问题