【问题标题】:URL rewriting : Apache configuration to prevent loading default folder index.htmlURL 重写:Apache 配置以防止加载默认文件夹 index.html
【发布时间】:2013-09-27 16:38:17
【问题描述】:

使用完全相同的 .htaccess 文件,我的本地 LAMP 服务器(通过 mamp pro)和生产服务器的行为不同。我想有一个不同的配置,但是哪一个?

在我的本地服务器上,http://domain.com/section/item/ 正确重定向到 http://domain.com/index.php?section=$1&item=$2

在我的生产服务器上,http://domain.com/section/item/ 提供对http://domain.com/section/item/index.html 的访问权限

我可以做些什么来让生产服务器像开发服务器一样工作?

这是 htaccess 文件内容,以防万一。

<IfModule mod_rewrite.c>

RewriteEngine on
Options +FollowSymLinks

#--------------------------------------------
# PRETTY URLS
#--------------------------------------------


# if the following conditions are met, SKIP the rewriteRules.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]

# Externally redirect to add missing trailing slash
RewriteRule [^/]$ %{REQUEST_URI}/ [R,L]

# SIX PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2&menu=$3&content=$4&id=$5&title=$6 [NC,L,QSA]

# FIVE  PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2&menu=$3&content=$4&id=$5 [NC,L,QSA]

# FOUR PARAMS
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2&menu=$3&content=$4 [QSA,L]

# THREE PARAMS : projects/touch/texts/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2&menu=$3 [QSA,L]

# TWO PARAMS: downloads
RewriteRule ^downloads/([^/]+)/$ index.php?section=downloads&item=$1 [QSA,L]

# TWO PARAMS:
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?section=$1&item=$2 [QSA,L]

# TAG URL : index.php?tag=url+encoded+keyword
RewriteRule ^tag/([\w-]+)/$ index.php?tag=$1 [NC,L,QSA]

# ONE PARAM
RewriteRule ^([\w-]+)/$ index.php?section=$1 [L,QSA]

#--------------------------------------------
# END PRETTY URLS
#--------------------------------------------

</IfModule>

【问题讨论】:

  • 开发服务器和生产服务器上是否都存在/section/item/index.html
  • 是的,两个文件目录结构完全一样。

标签: apache .htaccess mod-rewrite


【解决方案1】:

尝试进行 2 项更改。

将此行添加到您的 .htaccess 文件顶部:

DirectoryIndex index.php

然后将您的第一条规则更改为:

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

编辑:也试试这个:

Options +FollowSymLinks -MultiViews

【讨论】:

  • 访问文件夹domain.com/section/item(在两台服务器上)时出现 403 禁止。
  • 这意味着您在开发主机和生产主机上都有index.html
  • 检查我的编辑。如果您仍然得到 403,请注释掉建议的 DirectoryIndex index.php
  • 我尝试了您的建议,似乎设置 DirectoryIndex index.php+ Options +FollowSymLinks -MultiViews 可以解决问题,谢谢!!!
  • MultiViews 是 Apache 自己在不明确使用 mod_rewrite 规则的情况下制作漂亮 URL 的不太好的方法。任何使用mod_rewrite 的人都应该关闭MultiViews 以避免这些冲突。
【解决方案2】:

是的,两个文件目录结构完全相同

我不知道您是如何准确设置所有内容的,但可能开发和生产中加载模块的顺序不同(远见),这可能会影响订单模块正在 URL 处理管道中应用。它的顺序或类似 mod_autoindex 或 mod_dir 的东西不同,因此导致/section/item/ 的请求被解析为/section/item/index.html 之前 mod_rewrite 有机会做任何事情。这意味着这个条件成立:

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]

您的其他规则都没有得到应用,因​​此/section/item/index.html 得到了服务。

至于如何解决此类问题,我想您可以尝试在服务器配置中关闭目录索引(通过将其注释掉,无处不在),或者将其设置为他们正在服务的目录中不存在的东西:

DirectoryIndex _index.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多