.htaccess RewriteRule Mac OSX 服务器存在问题,在 Unix 服务器上不存在
我正在开发一个飞机网站,并使用 .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()的已加载模块部分)
- 我有AllowOverride Allhttpd.conf 文件中(在适当的标签中),
- 我的服务器 API 是 Apache 2.0 Handler
我已经环顾了一段时间,但没有发现太多。我尝试了这个SO问题中的第一个解决方案,但添加的行导致我的日志中出现此错误:
/my_specific/document_root/.htaccess: DocumentRoot not allowed here
有什么建议吗?
更新:
事实证明,Apache 的多视图功能已打开,最终对我的大部分重写规则都起作用,但覆盖了它们并导致了列出的 4 个失败。我所要做的就是将其添加到我的 htaccess 文件的顶部:
Options -MultiViews
I'm working on an aircraft website and using a .htaccess file to rewrite the URLS so they are seo friendly. Here is the file:
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
This has worked on a *nix-based server as well as on MAMP in my local development environment, however upon migrating to a Mac OSX server, several (but not all) of the rules fail. In particular, these fail:
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
Other notes:
- the mod_rewrite module successfully loads (shows up in the Loaded Modules section of phpinfo())
- I have AllowOverride All in my httpd.conf file (in appropriate tags)
- my server API is Apache 2.0 Handler
I have looked around SO for a while but haven't found much. I tried the first solution in this SO question but the added line caused this error in my logs:
/my_specific/document_root/.htaccess: DocumentRoot not allowed here
Any suggestions?
UPDATE:
Turns out Apache's Multiviews feature was turned on which ended up working for most of my rewriterules anyway, but was overriding them and caused the 4 failures listed. All I had to do was add this to the top of my htaccess file:
Options -MultiViews
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,Apache 的多视图功能在默认情况下是打开的,无论如何,它最终都适用于我的大部分重写规则,但覆盖了它们并导致了列出的 4 个失败。我所要做的就是将其添加到我的 htaccess 文件的顶部:
Turns out Apache's Multiviews feature was turned on by default which ended up working for most of my rewriterules anyway, but was overriding them and caused the 4 failures listed. All I had to do was add this to the top of my htaccess file:
以下是我将如何更改您的重写规则:
顺便说一句,我很遗憾地说,但您的设计还有改进的空间。
一般来说,人们将全部重定向到
index.php
,然后用Php代码解码URL
。想想吧;)
Here's how I would change your rewriterules:
By the way I'm sorry to say that, but there's room for improvement in your design.
In general people redirect all to
index.php
then decode theURL
in Php code.Think about it ;)