2012-07-21 116 views
1

这里是我需要的。我有url pearlsquirrel.com/profilecomments.php?u=eggo。 eggo是我的用户名和动态网址的变化部分。我想重写url说pearlsquirrel.com/eggo/comments,使用.htaccess。用正斜杠重写.htaccess

这是我到目前为止有:

RewriteRule ^(.*)$ $1.php 
RewriteRule ^([a-zA-Z0-9_-]+)$ profilecomments.php?u=$1 
RewriteRule ^([a-zA-Z0-9_-]+)/$ profilecomments.php?u=$1 
RewriteCond %{HTTP_HOST} ^www\.pearlsquirrel\.com$ [NC] 
RewriteRule ^(.*)$ http://pearlsquirrel.com/$1/comments [L,R=301] 

,但我不能得到它的工作。任何帮助将不胜感激。谢谢!

+1

为什么不能让PHP脚本重定向到那里? – C0deH4cker 2012-07-21 16:25:00

回答

1
RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^/\.]+)/comments$ profilecomments.php?u=$1 [L] 

注意

如果你已经使用相对路径为你的图片,样式等,就需要改变这些成绝对路径,或者使用服务器的根文件夹作为基础相对路径,以获得您的网站可以正常显示。

例如,它会认为images/image.png/eggo/comments/images/image.png

但是,如果你不是前面添加一个斜杠/images/image.png文件路径将始终从服务器的根文件夹开始,您的网站将不会搞砸你的时候重新编写你的URL。

+0

这似乎工作,但由于某种原因,当我使用这种方法没有图像适当渲染。 http://pearlsquirrel.com/eggo/comments – Eggo 2012-07-21 16:31:57

+0

@Eggo - 这是因为你已经使用相对路径为您的图像和样式表等使用您的文件的文件夹作为基地。 (例如src =“images/image.png”)为了让包含的文件和图像能够正常工作,必须更改为绝对路径或相对于服务器根文件夹(例如src =“/ images/image.png” ) – Tom 2012-07-21 16:36:37

+0

这很有道理,非常感谢你的帮助。 – Eggo 2012-07-21 16:38:33

0

您的第一条规则将覆盖所有其他规则。 你所描述(如果我理解正确的),你需要处理由profilecomments.php /用户/评论?U =用户

RewriteRule ^([a-zA-Z0-9_-]+)/comments profilecomments.php?u=$1 [L] 

这应该这样做。