【问题标题】:.htaccess 404 error on real path真实路径上的.htaccess 404错误
【发布时间】:2011-11-18 02:15:16
【问题描述】:

我想要以下:

  1. www.bla-bla.com/ --> www.bla-bla.com/php/index.php
  2. www.bla-bla.com/php/index.php --> www.bla-bla.com/php/error.php

我尝试了以下方法但不起作用

RewriteEngine on
RewriteRule ^/?$  /php/index.php [S=1]
RewriteCond %{REQUEST_URI} =/php/index\.php
RewriteRule (.*)? /php/error.php [R=404]

如何拒绝来自真实路径的访问?


URL=http://localhost
 (3) [perdir C:/xampp/htdocs/] strip per-dir prefix: C:/xampp/htdocs/ -> 
 (3) [perdir C:/xampp/htdocs/] applying pattern '/php/index\.php' to uri ''
 (3) [perdir C:/xampp/htdocs/] strip per-dir prefix: C:/xampp/htdocs/ -> 
 (3) [perdir C:/xampp/htdocs/] applying pattern '^(/?)$' to uri ''
 (2) [perdir C:/xampp/htdocs/] rewrite '' -> '/php/index.php'
 (1) [perdir C:/xampp/htdocs/] internal redirect with /php/index.php [INTERNAL REDIRECT]
 (3) [perdir C:/xampp/htdocs/] strip per-dir prefix: C:/xampp/htdocs/php/index.php -> php/index.php
 (3) [perdir C:/xampp/htdocs/] applying pattern '/php/index\.php' to uri 'php/index.php'
 (3) [perdir C:/xampp/htdocs/] strip per-dir prefix: C:/xampp/htdocs/php/index.php -> php/index.php
 (3) [perdir C:/xampp/htdocs/] applying pattern '^(/?)$' to uri 'php/index.php'
 (1) [perdir C:/xampp/htdocs/] pass through C:/xampp/htdocs/php/index.php
URL=http://localhost/php/index.php
 (3) [perdir C:/xampp/htdocs/] strip per-dir prefix: C:/xampp/htdocs/php/index.php -> php/index.php
 (3) [perdir C:/xampp/htdocs/] applying pattern '/php/index\.php' to uri 'php/index.php'
 (3) [perdir C:/xampp/htdocs/] strip per-dir prefix: C:/xampp/htdocs/php/index.php -> php/index.php
 (3) [perdir C:/xampp/htdocs/] applying pattern '^(/?)$' to uri 'php/index.php'
 (1) [perdir C:/xampp/htdocs/] pass through C:/xampp/htdocs/php/index.php

选项 +FollowSymlinks 重写引擎开启

RewriteRule /php/index\.php /php/error.php [L]
RewriteRule ^(/?)$ /php/index.php [QSA,L]

这里是怎么回事?

【问题讨论】:

  • 您的意思是说bla-bla.com/ 中的所有内容都应该重写为bla-bla.com/php/ 中的相同内容,但是您不希望用户能够通过直接转到bla-bla.com/php/ 来访问任何内容?
  • 这几乎是我的主要目标,但我特别希望我向 Thx 寻求回复
  • 当访问者想要浏览 bla-bla.com/ 时,apache 执行并发送 php/index.php 文件,但是当访问者想要浏览 bla-bla.com/php/index.php 时,apache 执行并搜索 bla-bla.com/php/error.php 文件..
  • Is there a way to force apache to return 404 instead of 403? 的可能重复项 - 您可能正在寻找 L 标志。
  • RewriteRule ^/?$ /php/index.php [L] RewriteRule ^/?php/index\.php$ /php/error.php 即使这行解决了问题..我有一个立即安装 xampp..

标签: php .htaccess mod-rewrite


【解决方案1】:

使用

ErrorDocument 404 /php/error.php

在 .htaccess 文件中

【讨论】:

    【解决方案2】:

    试试这个:

    # If the actual request is for /php/index.php, rewrite to error.php
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /php/index.php  [NC]
    RewriteRule ^php/index\.php /php/error.php  [L]
    
    # Otherwise check if root request and rewrite to /php/index.php
    RewriteRule ^$ /php/index.php  [L]
    

    编辑:回复 cmets:

    如果没有 [L] 标志,它将尝试应用下一条规则。 mod_rewrite 将始终在 URI 被重写后通过重写引擎将其发回。它将继续这样做,直到 URI 不变地通过引擎。第一个条件检查 实际请求 是针对 /php/index.php,因此重写的 URI 将不匹配。示例:

    1. 有人请求http://bla-bla.com/
    2. 第一条规则不匹配,因为 HTTP 请求是 GET / HTTP/1.1
    3. 第二条规则重写为 /php/index.php
    4. mod_rewrite 通过重写引擎将重写后的 URI 发回
    5. 第一个规则不匹配,因为即使 URI 现在是“/php/index.php”,原始 HTTP 请求仍然相同GET / HTTP/1.1
    6. 第二条规则不匹配,因为 URI 现在是“/php/index.php”而不是“/”。
    7. 重写引擎停止,因为基本 URI 未更改。

    示例 2:

    1. 有人请求http://bla-bla.com/php/index.php
    2. 第一个规则匹配,因为 HTTP 请求是 GET /php/index.php HTTP/1.1
    3. URI 被重写为/php/error.php
    4. 第二条规则不匹配
    5. URI 通过重写引擎发回
    6. 第一个规则的条件匹配,因为 HTTP 请求仍然相同
    7. 但是,URI 现在不是“/php/index.php”,因此实际规则不匹配
    8. 第二条规则仍然不匹配
    9. reiwrte 引擎停止,因为基本 URI 未更改。

    【讨论】:

    • httpd.apache.org/docs/current/rewrite/flags.html#flag_l 说:有可能在处理重写的请求时,可能会再次遇到 .htaccess 文件或 部分,因此可能会从头开始再次运行规则集。
    • 所以,[L] 标志的意思是:GO 结束,然后从新的 URL 重新开始如果任何规则不匹配停止处理..angryyyyy
    • 查看我的编辑以解释发生了什么。另请注意,需要调整第一条规则。再试一次,看看是否仍然出现错误。
    • 很好的解释,出了什么问题.. RewriteCond %{THE_REQUEST} ^[AZ]+ /php/index.php [NC] 必须是 RewriteCond %{THE_REQUEST} ^[AZ]+/php /index.php [NC] ?上面的代码没有做我的工作..谢谢回复..
    • 对不起,那行忘记了反斜杠
    【解决方案3】:

    乔恩:我不明白你的解决方案。

    这是我的:首先检查是否有人尝试访问您不想要的内容(即/php/index.php),如果是,则重定向到错误并停止一切。然后之后(仅之后),你确定它不是/php/index.php 所以应用你的重定向:

    RewriteRule /php/index\.php /php/error.php [L]
    RewriteRule / /php/index.php [QSA,L]
    

    请记住,[L] 指令将立即停止 RewriteRules,因此如果要添加其他 RewriteRules,请删除最后一个。

    当然,如果您想将 ALL 传入请求重定向到 php/index.php 并将 URL 作为参数传递,这是 应该 工作的解决方案(我说“应该”,因为我还没有测试过):

    RewriteRule /php/index\.php /php/error.php [L]
    RewriteRule (.*) /php/index.php?url=$1 [QSA,L]
    

    奥利维尔

    【讨论】:

      猜你喜欢
      • 2021-07-09
      • 2015-09-21
      • 1970-01-01
      • 2017-02-08
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2018-07-13
      • 1970-01-01
      相关资源
      最近更新 更多