根据文件是否存在,通过.htaccess重定向所有URL和文件请求


Redirecting all URL and file requests through .htaccess based on whether file exists or not

以下是我要做的:

  • 当从文件系统请求一个文件,但该文件不存在时,将URL重写为/index.php?404
  • 当文件被请求并且它确实存在于文件系统中时,将URL重写为/index.php?文件
  • 在其他情况下,是否将URL重写为/index.php?数据

但我因此得到了500个错误,有人知道问题可能在哪里吗?我过去使用过RewriteEngine,但对于如何在这种特殊情况下使用它,我仍然有点困惑。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} (.*'.([a-zA-Z]{2,4}))$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php?404 [L]
RewriteCond %{REQUEST_FILENAME} (.*'.([a-zA-Z]{2,4}))$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* ./index.php?file [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php?data [L]

您有无限的重写循环。若要解决,请添加不重写已重写URL的额外条件。。或者至少忽略对CCD_ 1的请求。

可能的方法之一:

# do not touch any requests to index.php
RewriteRule ^index'.php$ - [L]

p.S.L标志的工作原理:RewriteRule Last[L]标志不工作?