apache - 多个 htaccess 中的 mod_rewrite 协同工作

标签 apache .htaccess mod-rewrite url-rewriting

我有一个使用 mod_rewrite 的本地 Intranet,它有一个运行用户应用程序的框架,在某些情况下也需要 mod_rewrite,但我无法将两者结合起来很好地协同工作。

我希望特定的应用程序有自己的 .htaccess 来重写 URL,而不用担心框架需要执行的重写。因此,应用程序应该重写部分 URL 并让框架从那里接管。

先上代码:

/intranet/apps 中的应用程序框架.htaccess:

RewriteEngine On
RewriteRule   ^([^\/]+)\/([^\/.]+)\/\?(.+)$   $1/index.php?screen=$2 [QSA,NC,L]
RewriteRule   ^([^\/]+)\/([^\/.]+)\/?$        $1/index.php?screen=$2 [QSA,NC,L]

/intranet/apps/myapp 中特定于应用程序的 .htaccess:

RewriteEngine On
RewriteOptions inherit
RewriteRule   ^export\/([0-9]+)\/?$   export/?pid=$1 [QSA,NC,L]

此应用程序具有导出功能,可以接受特定的 ID 进行处理。所以,要打破这个:

网址:www.intranet.com/apps/myapp/export/99

应该重写为

网址:www.intranet.com/apps/myapp/export/?pid=99

通过特定于应用程序的 .htaccess。 现在,框架应该接管:

网址:www.intranet.com/apps/myapp/export/?pid=99

网址:www.intranet.com/apps/myapp/index.php?screen=export&pid=99

我的应用程序运行良好,因此框架会按预期进行重写,但是一旦特定于应用程序的重写进入游戏,该页面就会崩溃并返回 404

据我了解,此方案首先处理特定于应用程序的重写,然后通过 RewriteOptions inherit,接下来处理父级的重写。

令人沮丧的是,仅在特定于应用程序的 .htaccess 中启用 RewriteEngine on 就足以将其分解为位。我显然遗漏了一些东西,但我似乎无法解决。

最佳答案

问题可能是继承的规则在 myapp 的上下文中不起作用。您需要 ^([^\/.]+)\/?$ 内部 myapp 的正则表达式,因为应用于这些规则的 URI 将是 export/,而不是 myapp/export/(因为规则在 myapp 中)。

因此,要么将其添加到应用程序框架的 htaccess 文件(在文件末尾),要么添加到 myapp 中:

RewriteCond %{REQUEST_FILENANE} !-f
RewriteCond %{REQUEST_FILENANE} !-d
RewriteRule ^([^\/.]+)\/?$ index.php?screen=$1 [QSA,L]

关于apache - 多个 htaccess 中的 mod_rewrite 协同工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26186231/

相关文章:

php - 只允许通过应用程序访问 PHP 文件

php - 带有 .htaccess 或 PHP 的动态子域页面

php - URL 和 mod_rewrite : use many special chars and keep data safe from attacks

regex - 使用 RewriteRule 从重定向中删除主题标签

apache - 如何使用 .htaccess 规则将 .php 重写为 .html?

Apache Mod_rewrite 301 重定向

PHP 似乎可以在终端中使用,但不能在浏览器中使用

linux - OpenSSL 如何禁用和/或卸载

php - 使用 Joomla SEF 重写自定义 url

apache - 如何将 HDFS namenode 移动到新主机