【问题标题】:Not able to track htaccess issue, How to implement IF ELSE in htaccess无法跟踪 htaccess 问题,如何在 htaccess 中实现 IF ELSE
【发布时间】:2017-08-25 15:37:40
【问题描述】:

我在使用 htaccess 执行规则时遇到问题,如果规则匹配,则不要检查其他规则。我试图从过去几个小时中弄清楚。下面是示例代码

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase / 
   RewriteCond %{REQUEST_URI} ^/projectname/$
   #RewriteCond %{REQUEST_URI}  !projectname/(storage)$
   RewriteRule ^(.*)$ /projectname/abc/dist/$1 [L] 
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f 

   RewriteCond %{REQUEST_URI}  projectname/(storage)$ 
   RewriteRule ^(.*)$ /projectname/storage/$1 [L]

   Rewritecond %{REQUEST_URI} projectname/(.*)$ 
   #RewriteCond %{REQUEST_URI}  !projectname/(storage)$ [NC]
   RewriteCond %{REQUEST_URI}  !\.(html?|png|woff|ttf|eot|svg|woff2|jpg|gif|xml|rss|png|css|js|json)$ [NC]
   RewriteRule ^(.*)$ /projectname/#/$1 [NE,R=301,L]
   RewriteRule ^(.*)$ /projectname/abc/dist/$1 [L]

我想从 projectname/abc/dist 文件夹中加载所有内容,但不适用于当我在 url 中有存储时,我只想从存储文件夹中加载数据。 因此,根据此处定义的规则,一切正常,但是当我在 url/src 中存储图像时,它仍然检查 /projectname/abc/dist/storage/xxxx.png 而不是检查 htaccess 中定义的 /projectname/storage/xxxx.png规则。

我已尝试使用 [S=5] 跳过与 htaccess 中的存储规则不工作从参考 http://httpd.apache.org/docs/2.4/rewrite/flags.html

根据我的理解 [L] 是最后一个,所以它应该在存储规则之后停止 htaccess 但它没有。

我尝试在 htaccess 中实现 IF ELSE,但我为 IF ELSE 尝试的示例甚至没有帮助。 参考https://blogs.apache.org/httpd/entry/new_in_httpd_2_4 参考http://httpd.apache.org/docs/2.4/rewrite/flags.html(检查跳过 IF ELSE 节)

任何想法都会有用。

【问题讨论】:

  • 尝试在网页的 head 部分添加
  • 已经添加解决了重新加载问题。 @starkeen。没有可能是错过添加/projectname/。让我试试。
  • 我的回答对你有用吗?希望得到简短的反馈:)
  • 不,它不起作用@Hello Fishy .. 它阻止了 Angular 的加载。我现在已经通过将 Angular 放在与 laravel 不同的目录中进行整理。但是,是的,我将寻找解决我提到的问题的方法。一旦我得到它,我会在这里留下一个解决方案。

标签: apache .htaccess mod-rewrite


【解决方案1】:

您想从“projectname/abc/dist”文件夹加载,除非在请求的 URI 中的任何地方都有“storage”?!如果我做对了,那可能会有所帮助:

RewriteEngine On
RewriteBase / 

RewriteCond %{REQUEST_URI} !storage
RewriteRule ^(.*)$ /projectname/abc/dist/$1 [END,P]

假设您不想向客户展示他是由您的服务器重写的,我为“代理”添加了 [P]。如果您不介意看到它们,请忽略它。

【讨论】:

  • 这太令人惊讶了 oO 因为 END 与 L 相比,完全停止处理该请求的重写:使用 [END] 标志不仅终止当前轮次的重写处理(如 [L])而且还可以防止任何后续的重写处理发生在每个目录(htaccess)上下文中。
【解决方案2】:

首先,L|last 并不意味着停止所有处理。

如果您在 .htaccess 文件或部分中使用 RewriteRule,那么了解规则的处理方式非常重要。其简化形式是,一旦处理了规则,重写的请求将被交回 URL 解析引擎,以对其进行处理。

另请参阅 Ruleset Processing 了解其工作原理。


要从/project/abc/dist加载,除非URL中有storage,你必须先检查storage

RewriteCond %{REQUEST_URI} /storage/
RewriteRule /([^/]*$ /project/storage/$1 [L]

然后

RewriteRule ^(.*)$ /project/abc/dist/$1 [L]

最后,为了防止重写循环,在规则前面加上

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

万事俱备

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

RewriteCond %{REQUEST_URI} /storage/
RewriteRule /([^/]*$ /project/storage/$1 [L]

RewriteRule ^(.*)$ /project/abc/dist/$1 [L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-11
    • 2017-01-20
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多