Mod_Rewrite和Clean URL部分工作

时间:2012-03-22 20:50:18

标签: .htaccess mod-rewrite clean-urls

编辑#2:我禁用了我的404页面,默认未找到的页面显示它正在寻找带有.php的网址:

“在此服务器上找不到请求的网址/events/2012.php”

所以现在我相信它正在寻找一个实际的文件,当它不存在时。任何人??

编辑#1:看起来循环重定向是固定的,但我仍然无法弄清楚为什么重写不适用于事件页面。 / events /工作得很好,但/ events / 2012 / title-id /转到404. / breweries /和arizona-breweries / brewery-name /也工作,所以我不确定我错过了什么。

我正在尝试使用events.php页面添加新的目录结构。事件/ 2012 / ... 我在breweries.php下有另一个目录结构,可以正常工作。

当我尝试加载一个事件时,它会重定向循环并说“重定向太多”,并且网址看起来像这样:

sitename.com/events/2012/title-of-event-3.php.php.php.php.php.php.php.php.php.php.php /

我认为6个小时的搜索答案就足够了。任何和所有帮助表示赞赏!

(并且我的真实htaccess文件中没有我没有sitename.com)

我的htaccess文件:

Options +FollowSymLinks -Multiviews
RewriteEngine on

RewriteBase /

#remove php file extension
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)/$ $1.php [QSA,L]

#clean urls
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^events/([0-9]+)/(.*)-([0-9]+)/$ events.php?year=$1&title=$2&id=$3 [R,NC,QSA]
RewriteRule ^(.*)-breweries/$ breweries.php?loc=$1 [NC,QSA]
RewriteRule ^(.*)-breweries/(.*)/$ breweries.php?loc=$1&brewery=$2 [NC,QSA,L]

#Force trailing slash after url
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.sitename.com/$1/ [L,R=301]

#force www
RewriteCond %{HTTP_HOST} !^www.sitename.com$
RewriteRule ^(.*)$ http://www.sitename.com/$1 [R=301,L]

3 个答案:

答案 0 :(得分:1)

问题是规则1和5的互动。

规则5在任何未映射到文件名的URI之后强制/

http://sitename.com/events/2012/title-of-event-3.php.php ->
    http://sitename.com/events/2012/title-of-event-3.php.php/

然后是规则1地图

http://sitename.com/events/2012/title-of-event-3.php.php/ ->
    http://sitename.com/events/2012/title-of-event-3.php.php.php

问题是你如何首先陷入这个循环?我不确定为什么因为events/2012/title-of-event-3/会与^events/([0-9]+)/(.*)-([0-9]+)/匹配,但我已经看到了这个非预期的子查询,所以我建议您消除触发子查询搞砸逻辑的可能性通过添加第一条规则:

RewriteCond %(IS_SUBREQ}%{ENV:REDIRECT_END} y|1
RewiteRule  ^                           -    [L]

并将E=END:1添加到您要终止重写周期的规则标记中。

答案 1 :(得分:0)

我解决了。我不得不移动删除.dp扩展名下面的url重写部分(在强制www部分上方)的部分,并且它有效。显然啤酒厂部门曾经一度在正确的地方,所以它继续工作,但当我试图添加一个新的重写时,它将无法正常工作。

答案 2 :(得分:0)

大家好,我认为这会有所帮助 您需要做的是允许某些排序映射,例如/ someurl / somescript“请求映射到/someurl/somescript.php 但是当你使用这种映射时,记得要排除一些可能属于目录的重要部分,例如/ someurl / css肯定会指向你的样式表文件夹,你不希望它被附加或映射到这样的东西上as /someurl/css.php<一旦执行重写规则。 使用它,它适用于/ someurl / somescript或/ someurl / dir / somescript或/ someurl / dir11 / dir2 / somescript

#prevents loops
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
#conditions below prevents directory 
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteCond %{REQUEST_FILENAME} !css$  [NC]
RewriteCond %{REQUEST_FILENAME} !js$  [NC]
#conditions below exclude some of the file extension that you might be using on your   system
RewriteCond %{REQUEST_URI} !\.ico$  [NC]
RewriteCond %{REQUEST_URI} !\.css$  [NC]
RewriteCond %{REQUEST_URI} !\.js$  [NC]
RewriteCond %{REQUEST_URI} !\.svg$  [NC]
RewriteCond %{REQUEST_URI} !\.png$  [NC]
RewriteCond %{REQUEST_URI} !\.jpeg$  [NC]
RewriteCond %{REQUEST_URI} !\.jpg$  [NC]
#condition checks  for any file that dont ends with .php which would have selected all the file that forced to exclude all the file not ending with .php thus including even dir that why to exclude them above
RewriteCond %{REQUEST_URI} !\.php  [NC]
# Map internally to the resource, showing the "pretty" URL in the address bar
#note that /? indicate zero or more of / and [^/] states do nt include / on the begining, the + indicates that one or more with fullstop preceding it means one or more of any character
#RewriteRule  ^([^/]+)/?   /$1.php  [NC]
 #note that the rule below is different from above because it allows one or more ^([^/]+)/? which is achieved by having it grouped by () and appending a plus 
RewriteRule  (^([^/]+)/?)+  %{REQUEST_URI}.php  [NC]