You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.htaccess多索引文件规则中RewriteCond不生效问题

Fixing .htaccess Rules for Targeted 404 and Proper Path Handling

Key Issues in Current Code

  • Missing coverage for /index.php: Your first rule only blocks / and /index.html, but you need to include /index.php to meet your requirement.
  • Incorrect rewrites for some-path: The rule rewriting some-path/... to /... sends requests to the root directory, which either hits the 404 block or points to non-existent files, causing all some-path requests to fail.
  • Misplaced .html rewrite target: The rule for rewriting paths like some-path/about to .html targets the root instead of the some-path directory.

Corrected .htaccess Code

RewriteEngine On

# Block root "/" and "/index.php" with 404
RewriteCond %{THE_REQUEST} ^GET\ /(index\.php)?\ HTTP/
RewriteRule ^index\.php?$ - [L,R=404]

# Block direct access to "/some-path/" (no trailing file) with 403
RewriteCond %{REQUEST_URI} ^/some-path/?$
RewriteRule ^ - [L,R=403]

# Rewrite clean paths in some-path to their corresponding .html files (within some-path)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^some-path/([^/.]+)$ /some-path/$1.html [L]

Explanation of Changes

  • Root 404 Block: Using %{THE_REQUEST} ensures we catch both direct requests for / and /index.php, avoiding issues with internal server rewrites (like Apache automatically loading index.php for /).
  • Removed Harmful Rewrite: The rule that moved some-path files to the root is removed—this allows Apache to serve files like /some-path/index.php directly from their correct directory.
  • Fixed .html Rewrite: Now rewrites paths like /some-path/about to /some-path/about.html, ensuring the file is looked up in the some-path directory instead of the root.

Testing

  • Access http://domain/ or http://domain/index.php: Returns 404.
  • Access http://domain/some-path/index.php: Serves the file from the some-path directory.
  • Access http://domain/some-path/about: Rewrites to /some-path/about.html (if the file exists).
  • Access http://domain/some-path/: Returns 403.

内容的提问来源于stack exchange,提问作者Kirill Zaytsev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 10:22:40