【问题标题】:.htaccess RewriteRule Issue with Mac OSX server, not present on Unix serverMac OSX 服务器的 .htaccess RewriteRule 问题,在 Unix 服务器上不存在
【发布时间】:2012-02-20 22:36:49
【问题描述】:

我正在研究飞机网站并使用 .htaccess 文件重写 URL,以便它们对 seo 友好。这是文件:

AddType application/x-httpd-php /(.*)
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^general-aviation-models/(.*)/(.*) /models.php?mfg=$1&model=$2
RewriteRule ^general-aviation/(.*)/(.*) /general.php?page=$1&sub=$2
RewriteRule ^general-aviation/(.*)  /general.php?page=$1
RewriteRule ^general-aviation  /index.php
RewriteRule ^military/(.*) /military.php?page=$1
RewriteRule ^military-models/(.*)/(.*) /military-models.php?mfg=$1&model=$2
RewriteRule ^military-models/(.*) /military-models.php?mfg=$1
RewriteRule ^cart/ /cart.php
RewriteRule ^contact/ /contact.php
RewriteRule ^commercial/(.*)/(.*) /commercial.php?page=$1&sub=$2
RewriteRule ^commercial/(.*)  /commercial.php?page=$1
RewriteRule ^commercial/ /commercial.php
RewriteRule ^links/ /links.php
RewriteRule ^about/ /about.php
RewriteRule ^tour/(.*)  /tour.php?page=$1
RewriteRule ^tour/ /tour.php

ExpiresDefault "access plus 10 years"
AddOutputFilterByType DEFLATE text/plain

这在我的本地开发环境中的基于 *nix 的服务器和 MAMP 上都有效,但是在迁移到 Mac OSX 服务器时,一些(但不是全部)规则失败。特别是,这些失败:

RewriteRule ^military-models/(.*)/(.*) /military-models.php?mfg=$1&model=$2
RewriteRule ^military-models/(.*) /military-models.php?mfg=$1
RewriteRule ^commercial/(.*)/(.*) /commercial.php?page=$1&sub=$2
RewriteRule ^commercial/(.*)  /commercial.php?page=$1

其他说明:

  • mod_rewrite 模块成功加载(显示在 phpinfo() 的 Loaded Modules 部分)
  • 我的 httpd.conf 文件中有 AllowOverride All(在适当的标签中)
  • 我的服务器 API 是 Apache 2.0 处理程序

我已经环顾了一段时间,但没有找到太多。我在this SO question 中尝试了第一个解决方案,但添加的行导致我的日志中出现此错误:

/my_specific/document_root/.htaccess: DocumentRoot not allowed here

有什么建议吗?

更新:

事实证明 Apache 的 Multiviews 功能已打开,无论如何它最终都适用于我的大多数重写器,但它覆盖了它们并导致了列出的 4 个故障。我所要做的就是将它添加到我的 htaccess 文件的顶部:

Options -MultiViews

【问题讨论】:

    标签: apache mod-rewrite multiviews


    【解决方案1】:

    原来 Apache 的 Multiviews 功能在默认情况下是打开的,这最终对我的大多数重写器都有效,但它覆盖了它们并导致了列出的 4 个故障。我所要做的就是将它添加到我的 htaccess 文件的顶部:

    Options -MultiViews
    

    【讨论】:

      【解决方案2】:

      以下是我将如何更改您的重写规则:

      AddType application/x-httpd-php /(.*)
      Options +FollowSymLinks
      RewriteEngine On
      
      RewriteRule ^/?$ /index.php [NC,QSA,L]
      RewriteRule ^(tour|cart|links|about|military(-models)?|general-aviation|commercial|contact)/?$ /$1.php [NC,QSA,L]
      
      RewriteRule ^general-aviation-models/([^/]+)/(.*) /models.php?mfg=$1&model=$2 [NC,QSA,L]
      RewriteRule ^general-aviation-models/(.+) /models.php?mfg=$1 [NC,QSA,L]
      
      RewriteRule ^general-aviation/([^/]+)/(.*) /general.php?page=$1&sub=$2 [NC,QSA,L]
      RewriteRule ^general-aviation/(.+)  /general.php?page=$1 [NC,QSA,L]
      
      RewriteRule ^military-models/([^/]+)/(.*) /military-models.php?mfg=$1&model=$2 [NC,QSA,L]
      RewriteRule ^military-models/(.+) /military-models.php?mfg=$1 [NC,QSA,L]
      
      RewriteRule ^commercial/([^/]+)/([^/]+) /commercial.php?page=$1&sub=$2 [NC,QSA,L]
      RewriteRule ^commercial/(.+)  /commercial.php?page=$1 [NC,QSA,L]
      
      RewriteRule ^military/([^/]+) /military.php?page=$1 [NC,QSA,L]
      RewriteRule ^tour/(.+)  /tour.php?page=$1 [NC,QSA,L]
      
      ExpiresDefault "access plus 10 years"
      AddOutputFilterByType DEFLATE text/plain
      

      顺便说一句,很抱歉,您的设计还有改进的余地。

      一般来说,人们将all重定向到index.php,然后在Php代码中解码URL

      考虑一下;)

      【讨论】:

      • 感谢您的回答!不幸的是,OSX 服务器仍然存在上述部分的问题。通过“解码 php 代码中的 URL,你的意思是解析它并使用 php 引导请求吗?我可以看到这将如何消除我的问题。你能引导我到 SO 文章或我可以看到一个例子的地方吗?谢谢!
      • 我无意将contact 更改为index,因为我遵循了您的重写规则。这只是我的评论,这可能会让你认为:)
      • 明白了。非常感谢你的帮助。我发现了很多关于前端控制器模式的东西,我认为它们很好地解决了这个问题。
      • 原来是打开了Apache的多视图功能,覆盖了.htaccess规则。我刚刚在我的 htaccess 文件的顶部添加了以下行:Options -MultiViews
      • 不错 =) 有时最小的东西最难找到!
      猜你喜欢
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      • 2014-01-01
      • 1970-01-01
      相关资源
      最近更新 更多