真实路径上的.htaccess 404错误

时间:2011-11-18 02:15:16

标签: php .htaccess mod-rewrite

我想要以下内容:

  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
  3. 我尝试了以下但不起作用

    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     

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

    Wtf正在进行中吗?

3 个答案:

答案 0 :(得分:1)

使用

ErrorDocument 404 /php/error.php

在.htaccess文件中

答案 1 :(得分:0)

试试这个:

# 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]

编辑:回复评论:

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

  1. 有人要求http://bla-bla.com/
  2. 第一条规则不匹配,因为HTTP请求为GET / HTTP/1.1
  3. 第二条规则重写为/php/index.php
  4. mod_rewrite通过重写引擎
  5. 发回重写的URI
  6. 第一条规则不匹配,因为虽然URI现在是“/php/index.php”,原始HTTP请求仍然相同GET / HTTP/1.1
  7. 第二条规则不匹配,因为URI现在是“/php/index.php”而不是“/".
  8. 重写引擎停止,因为基URI不变。
  9. 示例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. 发回
    7. 第一条规则的条件匹配,因为HTTP请求仍然相同
    8. 但是,URI现在不是'/php/index.php'所以实际规则不匹配
    9. 第二条规则仍然不匹配
    10. reiwrte引擎停止,因为基URI不变。

答案 2 :(得分:0)

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

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

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

请注意, [L] 指令会立即停止RewriteRules,因此如果要添加其他RewriteRules,请删除le last。

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

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

奥利弗