【问题标题】:Mod_Rewrite and Clean URLs Partially WorkingMod_Rewrite 和清理 URL 部分工作
【发布时间】:2012-03-22 20:50:18
【问题描述】:

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

“在此服务器上未找到请求的 URL /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/ 时,它会重定向循环并显示“重定向过多”,并且 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]

【问题讨论】:

    标签: .htaccess mod-rewrite clean-urls


    【解决方案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 添加到您要终止重写周期的规则标志中。

    【讨论】:

    • 好的,现在我们到了某个地方,我不得不使用您的代码的修改版本,因为它一直给我一个 500 错误:RewriteCond %{ENV:REDIRECT_STATUS} !^$ [OR]RewriteCond %{IS_SUBREQ} =trueRewriteRule .* - [L] 这停止了循环重定向但我仍然无法访问 /events/2010/title-3/。但是现在,它只显示正常的 404 Not Found 而不是 .php.php.php 等。
    • 你的 docroot 中有 events.php 吗?
    • 是的,events.php 在根文件夹中。我完全被难住了。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      大家好,我认为这会有很大帮助 您需要做的是允许将某种请求(例如 /someurl/somescript" 的排序映射映射到 /someurl/somescript.php 但是当您使用这种映射时,请记住排除一些可能属于目录的重要部分,例如 /someurl/css 肯定会指向您的样式表文件夹,并且您不希望它被附加或映射到这样的东西作为 /someurl/css.php

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

      【讨论】:

        猜你喜欢
        • 2010-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多