简体   繁体   English

Apache htaccess 重写动态重写库

[英]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)

  • ! = Not = 不是
  • (?: = Create non-capture group which starts with / and follows A-Za-z or numeric (?: = 创建以 / 开头并跟随 A-Za-z 或数字的非捕获组
  • )? )? = On end of the non-capture is the question mark which makes it optional, so it can have this group or can not = 在非捕获的末尾是一个问号,它使它成为可选的,所以它可以有这个组或不能
  • (jpe?g.... = capture group so it should contain this extension (jpe?g.... = 捕获组所以它应该包含这个扩展名

RewriteRule重写规则

^([A-Za-z0-9]+\/)?admin_cms/?(.*)$ $1admin.php?rt=$2&%{QUERY_STRING}
  • Makes the first group now as a capture, but it is optional, it will also appends the /.现在将第一组作为捕获,但它是可选的,它还将附加 /。 Position the / outside the optional group will make the pattern not work with an root url.将 / 放在可选组之外将使模式不适用于根 url。
  • Then for the rewrite we put the $1 as first group at the beginning然后对于重写,我们将 $1 作为第一组放在开头
  • $2 will be the part that will be passed to the rt $2 将是将传递给 rt 的部分

It will results in:这将导致:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM