高级 htaccess 重定向
Advanced htaccess Redirection
使用 mod_rewrite,我有一系列服务器端重定向(这意味着客户端不知道 URI 更改。)我想这样做,以便任何客户端引用 index.php return 404 错误,不会中断服务器端请求...
--
示例..
客户端请求x.html,加载index.php的内容..
客户端请求 index.php,客户端收到 404 未找到错误..
--
我知道这个格式不好,可能可以解释得更好,但我希望这个解释足够好 - 如果没有,我道歉...
--
我在 google 上花费了最后三个小时,试图找到解决这个问题的方法,但由于我真的不知道它会被理解为什么,所以结果很短...
答案取决于您的上下文:如果您 不是 .htaccess
或 <Directory>
,这很容易:
RewriteEngine On
RewriteRule /index.php - [L,R=404]
RewriteRule /x.html /index.php [L]
如果您 在 .htaccess
或 <Directory>
中,请求将被第二次处理,请参阅 here。
我使用了一个环境变量来解决这个问题:
RewriteEngine On
# after an internal redirect, the variable is prefixed with REDIRECT_
RewriteCond %{ENV:REDIRECT_redirect_to_php} !=true
# no leading / this time
RewriteRule index.php - [L,R=404]
RewriteRule x.html /index.php [L,E=redirect_to_php:true]
您可以使用RewriteLog and RewriteLogLevel获取重写执行的详细信息。
也许 [END] 标签就是您要搜索的内容。正如大卫提到的,问题是请求被第二次处理。据我所知,您可以使用 [END] 来防止这种行为。请参阅 (在答案末尾)或 mod_rewrite 文档:
Using the [END] flag terminates not only the current round of rewrite processing (like [L]) but also prevents any subsequent rewrite processing from occurring in per-directory (htaccess) context.
http://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_end
需要 Apache 2.3.9 或更高版本。
使用 mod_rewrite,我有一系列服务器端重定向(这意味着客户端不知道 URI 更改。)我想这样做,以便任何客户端引用 index.php return 404 错误,不会中断服务器端请求...
--
示例..
客户端请求x.html,加载index.php的内容..
客户端请求 index.php,客户端收到 404 未找到错误..
--
我知道这个格式不好,可能可以解释得更好,但我希望这个解释足够好 - 如果没有,我道歉...
--
我在 google 上花费了最后三个小时,试图找到解决这个问题的方法,但由于我真的不知道它会被理解为什么,所以结果很短...
答案取决于您的上下文:如果您 不是 .htaccess
或 <Directory>
,这很容易:
RewriteEngine On
RewriteRule /index.php - [L,R=404]
RewriteRule /x.html /index.php [L]
如果您 在 .htaccess
或 <Directory>
中,请求将被第二次处理,请参阅 here。
我使用了一个环境变量来解决这个问题:
RewriteEngine On
# after an internal redirect, the variable is prefixed with REDIRECT_
RewriteCond %{ENV:REDIRECT_redirect_to_php} !=true
# no leading / this time
RewriteRule index.php - [L,R=404]
RewriteRule x.html /index.php [L,E=redirect_to_php:true]
您可以使用RewriteLog and RewriteLogLevel获取重写执行的详细信息。
也许 [END] 标签就是您要搜索的内容。正如大卫提到的,问题是请求被第二次处理。据我所知,您可以使用 [END] 来防止这种行为。请参阅 (在答案末尾)或 mod_rewrite 文档:
Using the [END] flag terminates not only the current round of rewrite processing (like [L]) but also prevents any subsequent rewrite processing from occurring in per-directory (htaccess) context. http://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_end
需要 Apache 2.3.9 或更高版本。