RewriteRules 是否在 Apache Alias 内匹配

发布于 2024-11-05 08:41:46 字数 771 浏览 3 评论 0原文

我有一个在几个不同环境中运行的应用程序,有时它位于站点的顶层,有时它在别名内运行。

当它直接签出到普通文档根时,我的 .htaccess 是这样的:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]

我将站点部署在具有如下别名的环境中:

Alias /foo /path/to/my/application/

我发现我必须将 .htaccess 修改为以下内容:

RewriteEngine On
RewriteBase /foo
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /foo/index.php [NC,L]

我的问题是,如何我可以重写我的 .htaccess 以便无论是否使用别名,重写的效果都相同吗?有什么方法可以检测是否已设置别名,或者是否有其他方式来表述 RewriteRules?

I have an application that operates in a few different environments, sometimes it's at the top level in a site or sometimes it's operating inside an alias.

When it's a straight checkout to a normal docroot my .htaccess is something like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]

I deployed the site in an environment that had an alias like the following:

Alias /foo /path/to/my/application/

I found I had to modify the .htaccess to the following:

RewriteEngine On
RewriteBase /foo
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /foo/index.php [NC,L]

My question is, how can I rewrite my .htaccess so that the rewriting works the same whether it's under an alias or not? Is there some way I can detect if an alias has been set, or some other way of phrasing the RewriteRules?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枉心 2024-11-12 08:41:46

由于 mod_rewrite 优先于 mod_alias,因此您可能需要用重写规则替换别名。

Alias /foo /path/to/my/application/

将变成:

RewriteRule ^foo.*$ path/to/my/application$1 [L]

那么 .htaccess 文件中的规则(可能位于或不位于根目录)应该可以工作,而无需更改它们。

Since mod_rewrite takes precedence over mod_alias, you probably need to replace your aliases with rewrite rules.

This:

Alias /foo /path/to/my/application/

would become:

RewriteRule ^foo.*$ path/to/my/application$1 [L]

Then the rules in the .htaccess file that may or may not be at the root should work without you having to change them.

记忆消瘦 2024-11-12 08:41:46

如果您有权访问文档根目录,解决方案是创建符号链接:

ln -s /<path to application> /<document root>/foo

Solution is creating a symlink, if you have access to your document root:

ln -s /<path to application> /<document root>/foo
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文