RewriteRules 是否在 Apache Alias 内匹配
我有一个在几个不同环境中运行的应用程序,有时它位于站点的顶层,有时它在别名内运行。
当它直接签出到普通文档根时,我的 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于 mod_rewrite 优先于 mod_alias,因此您可能需要用重写规则替换别名。
这
将变成:
那么 .htaccess 文件中的规则(可能位于或不位于根目录)应该可以工作,而无需更改它们。
Since mod_rewrite takes precedence over mod_alias, you probably need to replace your aliases with rewrite rules.
This:
would become:
Then the rules in the .htaccess file that may or may not be at the root should work without you having to change them.
如果您有权访问文档根目录,解决方案是创建符号链接:
Solution is creating a symlink, if you have access to your document root: