我想要以下内容:
www.bla-bla.com/
- > www.bla-bla.com/php/index.php
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
上的RewriteEngineRewriteRule /php/index\.php /php/error.php [L]
RewriteRule ^(/?)$ /php/index.php [QSA,L]
Wtf正在进行中吗?
答案 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不匹配。示例:
GET / HTTP/1.1
GET / HTTP/1.1
示例2:
GET /php/index.php HTTP/1.1
/php/error.php
答案 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]
奥利弗