Apache mod_rewrite 用于除 root 之外的所有内容

标签 apache .htaccess mod-rewrite

嗨,我正在尝试编写一个 mod_rewrite 规则来重定向除根文件夹之外的所有内容。
例如,www.example.com 应该加载 index.html 文件
对于其他一切,例如www.example.com/tag,/tag 应该传递给子目录中的脚本

现在我有

RewriteCond %{REQUEST_URI} !^/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) app/webroot/$1 [L]

这很好地加载了 index.html 但/tag 没有传递给 webroot。我收到 404 错误。

谢谢

最佳答案

这个条件是问题:

RewriteCond %{REQUEST_URI} !^/

您所说的以 '/' 开头的任何内容都不会被重写,并且所有内容都以 '/' 开头。您需要使用 $在末尾:
RewriteCond %{REQUEST_URI} !^/$

不过,我不确定您是否需要该规则,因为如果 index.html 存在,其他两个规则将自动处理。只需使用这些来重写任何物理上不存在的东西:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ app/webroot/$1 [L,QSA]

而且您可以在您的应用程序中处理 404 错误,因为无论如何您都必须处理子目录。

关于Apache mod_rewrite 用于除 root 之外的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5437375/

相关文章:

java - http 客户端 4.25 中的 PostMethod$addParameter() 相当于什么

apache - HT访问: How to FILTER A STRING anywhere in the full URL string?

php - PHP 路由之谜

wordpress - 访问 WordPress 站点的子文件夹

apache - 为什么Apache将URL解码我的查询字符串?

apache - WordPress 空白页问题没有 www &/

apache - htaccess 将所有页面重定向到单个页面

apache - .htaccess 从 Host 重写到没有 uri 的静态站点

apache - apache 中的重定向

php - 将 mod_rewrite 与 php 文件一起使用以动态提供重写