【问题标题】:Apache is redirecting folder to file .html with the same nameApache 正在将文件夹重定向到同名的 .html 文件
【发布时间】:2017-01-02 04:22:03
【问题描述】:

我正在尝试将 404 json fales 重定向到 404.json(以“{}”作为内容)。但是 apache 正在将文件夹重定向到具有相同文件夹名称的 html 文件,并且任何自定义 404.json 重定向都会失败。

文件结构:

  • /example1/
  • /example2/
  • /example1.html
  • /example2/data.json
  • /data.json

工作(重定向到 404.html):

工作(重定向到 404.json):

工作(打开文件):

不工作(默认 apache 404):

似乎 apache 正在将“example1/”重定向到“example1.html”,尤其是当我尝试访问目录内的文件时,例如 http://example.com/example2/data.json,显示 404 消息:

在此服务器上找不到请求的 URL /example2.html/data.json。

即使没有 .htaccess,它也会发生!都是因为example1.html文件的问题。

htaccess:

# Rewrite MOD
Options +FollowSymLinks
RewriteEngine On

# If requested resource exists as a file or directory go to it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) - [L]

# 404 json
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)\.json$ 404.json [L]

# 404 page
RewriteRule . 404.html [L]

也许这是一些 apache 文件映射配置,但我找不到任何关于它的信息。它只发生在部署服务器上,对于 localhost 很好。 =/

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite


    【解决方案1】:

    在关闭MultiViews 的情况下进行这样的操作:

    Options +FollowSymLinks -MultiViews
    RewriteEngine On
    
    # If requested resource exists as a file or directory go to it
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    
    # 404 json
    RewriteRule \.json$ 404.json [L,NC]
    
    # 404 page
    RewriteRule ^ 404.html [L]
    

    【讨论】:

    • 天啊!它是如此简单,我花了几个小时试图修复它......非常感谢!
    • 感谢修复整个 .htaccess =)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 2017-06-09
    • 1970-01-01
    • 2013-03-17
    • 2023-04-01
    • 1970-01-01
    • 2012-02-06
    相关资源
    最近更新 更多