我有一个旧的项目,我有以下mod_rewrite条件/规则的问题:
RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L]
但是.phtml文件在浏览器中以纯文本forms呈现。
问题是我在一个公共目录下有一些.phtml文件,这些文件与第一个重写条件'-s' (是普通文件,大小)匹配,所以它们不会被发送到index.php。 实际上.phtml文件显示为纯文本! 我需要保留这些文件,所以我不能设置deny from all的目录deny from all ,因为它还包含一些图像和东西。
我试过这个:
RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L] RewriteRule \.phtml$ index.php [NC,L]
但它没有工作,因为它已经达到了第一个重写条件,所以第一个规则被应用(什么都不做)。
我怎样才能解决这个问题?
我认为这个规则和.htaccess不是一个条件。 你应该configurationapache tu使用PHP的phtml文件:
AddType application/x-httpd-php .php .phtml .html
那么规则
RewriteRule \.phtml$ index.php [NC,L]
可以ommited,它应该工作,因为文件存在和大小,所以Apache应该像处理任何其他PHP文件。
编辑:如果你不想提供.phtml文件,你可以使用类似的重写:
RewriteCond %{REQUEST_FILENAME} \.phtml$ RewriteRule ^.*$ index.php [NC,L] RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L]
你说:“但是它没有工作,因为它已经达到了第一个重写条件,所以第一个规则被应用了(什么也不做)。
这是非常正确的,因为规则中的L标志告诉Apache,如果它被应用,其他规则不应该被应用。
它通常用于redirect等,但在你的情况下,这个标志是不需要的。
试试这个configuration:
RewriteCond %{REQUEST_FILENAME} !-s [AND] RewriteCond %{REQUEST_FILENAME} !-l [AND] RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^.*$ index.php [NC,L]