【问题标题】:Rewrite path before processing directory?在处理目录之前重写路径?
【发布时间】:2010-07-11 01:25:06
【问题描述】:

我的 htaccess 文件有一个小问题。目前,它会将所有内容重定向回 index.php 进行处理,除非有人尝试访问实际目录。当目录存在时,它会显示 403 错误页面,而不是像预期的那样重写 index.php 的路径。我可以修改什么以使其在通过 Internet 访问文件时始终转到 index.php,但仍然在服务器上的 PHP 中加载正确的文件?

Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /

ErrorDocument 403 /index.php?403
ErrorDocument 404 /index.php?404
ErrorDocument 414 /index.php?414

RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteRule !^(.*) index.php [L]

示例文件结构:

Web Directory
- com
- source
- index.php
- TEST.HTML

“com”和“source”等文件夹将显示 403,因为用户无权访问它们。 'index.php' 和'TEST.HTML' 等文件正常执行。无论如何,我希望我的 htaccess 将 Web Directory 文件夹中的 everything 重定向回 index.php 文件。

【问题讨论】:

    标签: .htaccess mod-rewrite redirect filesystems


    【解决方案1】:

    我想你想要这个:

    Options -Indexes +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    
    ErrorDocument 403 /index.php?403
    ErrorDocument 404 /index.php?404
    ErrorDocument 414 /index.php?414
    
    RewriteCond %{REQUEST_URI} !^/index.php
    RewriteRule .* index.php [L]
    

    这是基于您不想直接访问TEST.HTML 并且不想更改用户浏览器中的 URL 的假设。如果其中任何一个假设是错误的,请告诉我,我将使用适当的重写信息更新答案。

    【讨论】:

    • 谢谢,你的例子帮助我意识到我在重写规则 RewriteRule !^(.*) index.php [L] 前面有否定。我不知道“不是任何字符 0 次或更多次”会对脚本产生什么样的奇怪效果。只需删除“!”解决了问题。
    猜你喜欢
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 2011-07-15
    • 2011-03-02
    相关资源
    最近更新 更多