[英]Apache htaccess rewrite dynamic rewritebase
I struggle with htaccess for production and local environment.我在生产和本地环境中使用 htaccess。 My production url is like: https://example.com/folder/file.php Local I use: https://example.dev.local/app/folder/file.php我生产的网址是这样的: https://example.com/folder/file.php地方我使用: HTTPS://example.dev.local/app/folder/file.php
As you can see my local environment is using app which is a subfolder.如您所见,我的本地环境正在使用 app,它是一个子文件夹。 I want to prevent to set RewriteBase /app/我想阻止设置 RewriteBase /app/
So affter searching I found answers like:因此,在搜索之后,我找到了以下答案:
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
However I can get it work for my local enviroment.但是我可以让它在我当地的环境中工作。 My htaccess is:我的 htaccess 是:
Options -Indexes
RewriteEngine On
# Redirect if not https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Urls ends with html
RewriteRule ^(.*).html$ index.php?rt=$1&%{QUERY_STRING} [L,QSA]
# requests which are not static sources will be handled for the admin environment
RewriteCond %{REQUEST_URI} !^admin_cms/\.*\.(jpe?g|css|js|gif|png|woff2?|ttf|ico)$ [NC]
RewriteRule ^admin_cms/?(.*)$ admin.php?rt=$1&%{QUERY_STRING} [L,QSA]
an URL like: https://example.dev.local/app/admin_cms/file.php should get matched for the second rule, but match only when I navigate to an url without app.像: https://example.dev.local/app/admin_cms/file.php这样的 URL 应该与第二条规则匹配,但仅当我导航到没有应用程序的 url 时才匹配。
So my question is, how do I get it work that both (prod / local) work with the same htaccess?所以我的问题是,如何让两者(产品/本地)使用相同的 htaccess 工作?
Ps.附言。 I tried also RewriteRule ^%{ENV:BASE}/admin_cms/?(.*)$ admin.php?rt=$1&%{QUERY_STRING} [L,QSA]
我也试过RewriteRule ^%{ENV:BASE}/admin_cms/?(.*)$ admin.php?rt=$1&%{QUERY_STRING} [L,QSA]
After digging I found an solution which worked for me.挖掘后,我找到了一个对我有用的解决方案。 Maybe there are other solutions as well, please post this so I can update my answer with the different possibilities.也许还有其他解决方案,请发布此信息,以便我可以用不同的可能性更新我的答案。
I made use of an non-capture group in my condition, and capture group in my rule.我在我的条件下使用了非捕获组,在我的规则中使用了捕获组。
# requests which are not static sources will be handled for the admin environment
RewriteCond %{REQUEST_URI} !^(?:\/[A-Za-z0-9]+)?/admin_cms/\.*\.(jpe?g|css|js|gif|png|woff2?|ttf|ico)$ [NC]
RewriteRule ^([A-Za-z0-9]+\/)?admin_cms/?(.*)$ $1admin.php?rt=$2&%{QUERY_STRING} [L,QSA]
To explain the rules:解释规则:
RewriteCond重写条件
!^(?:\\/[A-Za-z0-9]+)?/admin_cms/\\.*\\.(jpe?g|css|js|gif|png|woff2?|ttf|ico)
RewriteRule重写规则
^([A-Za-z0-9]+\/)?admin_cms/?(.*)$ $1admin.php?rt=$2&%{QUERY_STRING}
It will results in:这将导致:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.