【问题标题】:different 404 for subdirectory when paths are handled by index.php当路径由 index.php 处理时,子目录的不同 404
【发布时间】:2016-04-22 13:47:48
【问题描述】:
我希望 /blog/anything-that-doesn't-exist 重定向到 /blog,而任何其他 404 都由 Drupal 处理。
我的 htaccess 目前有:
ErrorDocument 404 /index.php
所以一切都由 Drupal 处理。是否可以有一个特定于子目录的 404 规则,但从根目录 htaccess 设置? /blog/ 子目录是由 Drupal 生成的,所以我不能在其中放置一个覆盖的 htaccess。
【问题讨论】:
标签:
.htaccess
redirect
drupal
http-status-code-404
subdirectory
【解决方案1】:
您可以在 Root/.htaccess 文件中使用以下代码:
RewriteEngine On
#If /blog/foo is not a directory
RewriteCond %{DOCUMENT_ROOT}/blog/$1 !-d
#And /blog/foo is not a file
RewriteCond %{DOCUMENT_ROOT}/blog/$1 !-f
#Then redirect /blog/foo to /blog
RewriteRule ^blog/(.+)$ /blog/ [NC,L,R]
值得注意的是,RewriteRule 和 ErrorDocument 指令的执行时间是不同的。 RewriteRule 指令在 ErrorDocument 指令之前执行,因此对 /blog/bad_url 的任何请求都将由上述规则处理,所有其他 404 请求将由 ErrorDocument 处理。