1

编辑#2:我禁用了我的 404 页面,默认的未找到页面说它正在寻找一个带有 .php 的 url:

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

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

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

我正在尝试使用我的 events.php 页面添加一个新的目录结构。events/2012/... 我在 breweries.php 下有另一个目录结构,它工作得很好。

当我尝试加载事件 sitename.com/events/2012/title-of-event-3/ 时,它会重定向循环并显示“重定向太多”,并且 URL 如下所示:

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]
4

3 回答 3

1

问题是规则 1 和 5 的相互作用。

规则 5 强制/在任何不映射到文件名的 URI 之后使用 a

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到您要终止重写周期的规则标志。

于 2012-03-22T21:33:10.557 回答
0

大家好,我认为这对您有很大帮助,您需要做的是允许某种类型的映射,例如 /someurl/somescript" 被映射到 /someurl/somescript.php 但当您使用这种映射时,请记住排除一些可能落入目录的重要部分,例如 /someurl/css 肯定会指向您的样式表文件夹,并且您不希望它被附加或映射到 /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]
于 2013-06-09T18:21:38.980 回答
0

我解决了。我不得不将删除 .php 扩展名的部分移到 url rewrite 部分下方(就在 force www 部分上方)并且它起作用了。显然,啤酒厂部分曾经在正确的位置,因此它继续工作,但是当我尝试添加新的重写时,它无法正常工作。

于 2012-03-29T19:49:43.573 回答