.htaccess RewriteRule Mac OSX 服务器存在问题,在 Unix 服务器上不存在

发布于 2024-12-29 17:15:06 字数 2111 浏览 5 评论 0原文

我正在开发一个飞机网站,并使用 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦亿 2025-01-05 17:15:06

事实证明,Apache 的多视图功能在默认情况下是打开的,无论如何,它最终都适用于我的大部分重写规则,但覆盖了它们并导致了列出的 4 个失败。我所要做的就是将其添加到我的 htaccess 文件的顶部:

Options -MultiViews

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:

Options -MultiViews
你是暖光i 2025-01-05 17:15:06

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

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

顺便说一句,我很遗憾地说,但您的设计还有改进的空间。

一般来说,人们将全部重定向到index.php,然后用Php代码解码URL

想想吧;)

Here's how I would change your rewriterules:

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

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 the URL in Php code.

Think about it ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文