【问题标题】:Removing index.php from url using htaccess使用 htaccess 从 url 中删除 index.php
【发布时间】:2023-03-07 13:52:01
【问题描述】:

您好朋友,我正在尝试从我的网站 URL 中删除 index.php,但它不起作用 为此我使用了.htaccess 文件,我做错了什么

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

【问题讨论】:

  • 如果没有 URL 中的 index.php,您的网站是否正常工作?
  • 否,显示错误禁止您无权访问此资源。此外,在尝试使用 ErrorDocument 处理请求时遇到 403 Forbidden 错误。添加 index.php 后,它工作正常
  • 您是如何设置网站的?您使用的是什么网络服务器(apache 或 nginx)?
  • 您的网址是否正常路由?
  • 你为什么不使用虚拟主机来防止这个问题。这是简单易行的解决方案或尝试使用gist.github.com/Guley/6b114d33eb7420a0a07d58670d965a8c,我主要使用这个

标签: php laravel .htaccess url


【解决方案1】:

尝试在 .htaccess 文件的顶部添加以下内容:

DirectoryIndex index.php

这指示 mod_dir 在请求目录时为 index.php 发出内部子请求。如果未正确设置或在请求的目录中未找到 index.php,则您将收到 403 响应。

这通常已经在大多数服务器上进行了配置,但是,Apache 的默认值仅是 index.html。如果这是默认设置,那么您在请求主页时会收到 403。

您的“前端控制器”依赖于正确设置的DirectoryIndex

【讨论】:

    猜你喜欢
    • 2021-10-02
    • 2015-01-20
    • 1970-01-01
    • 2011-05-20
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多