简体   繁体   English

Apache RewriteCond 不适用于 static 文件

[英]Apache RewriteCond not working for static files

I have two copies of the website in separate directories for different languages (Spanish and English) and the directory structure is as follows我在不同语言(西班牙语和英语)的不同目录中有两个网站副本,目录结构如下

+ /var/www/website
  |- es
  |- en

The es directory serves the Spanish version of the website and en serves the English version (default language). es目录服务于网站的西班牙语版本,而en服务于英文版本(默认语言)。

The URL schema would be like URL 模式就像

# English version
https://example.com/
https://example.com/en/ -> Redirects to https://example.com/

# Spanish version
https://example.com/es/

The static files are served from the respective directories only. static 文件仅从各自的目录提供。

Now, I have the following Apache2 configuration现在,我有以下Apache2配置

<VirtualHost *:443>
        # The primary domain for this host
        ServerName example.com

        DocumentRoot /var/www/website
        <Directory /var/www/website>
                Require all granted
                AllowOverride all

                RewriteEngine on
                RewriteBase /
                RewriteCond %{HTTP:Accept-Language} ^es [NC]
                RewriteRule ^$ /es/ [R]

                RewriteCond %{HTTP:Accept-Language} ^en [NC]
                RewriteRule ^$ /en/ [R]

                RewriteCond %{HTTP:Accept-Language} !^en [NC]
                RewriteCond %{HTTP:Accept-Language} !^es [NC]
                RewriteRule ^$ /en/ [R]
        </Directory>
</VirtualHost>

I'm facing a few problems with the configuration我在配置方面遇到了一些问题

  1. https://example.com/es/ and https://example.com/en/ are working but static files are not loading and the URL for the static files looks like https://example.com/image.png which has to be https://example.com/es/image.png (for Spanish) and https://example.com/en/image.png (for English) https://example.com/es/ and https://example.com/en/ are working but static files are not loading and the URL for the static files looks like https://example.com/image.png which必须是https://example.com/es/image.png (西班牙语)和https://example.com/en/image.png (英语)
  2. https://example.com/en/ should be redirected to https://example.com/ and the English website should be served, whereas the reverse is happening. https://example.com/en/应该被重定向到https://example.com/并且应该提供英文网站,而相反的情况正在发生。

Let's first understand what is happening here.让我们首先了解这里发生了什么。

With rules like this有了这样的规则

RewriteRule ^$ /es/ [R]

you are redirecting requests to / to /es/ .您正在将请求重定向到//es/

Redirecting means, the server will tell the browser to go search for the content at some other place.重定向的意思是,服务器会告诉浏览器go 在其他地方搜索内容。

The browser will then send another request to /es/ .然后浏览器将向/es/发送另一个请求。 This will not be rewritten nor redirected as there is no matching rule.这不会被重写或重定向,因为没有匹配的规则。

You html file apparently contains references with absolute URLs like this:您的 html 文件显然包含具有绝对 URL 的引用,如下所示:

<img src="/image.png" />

Your browser will therefore send a request for /image.png , which is not redirected, nor rewritten.因此,您的浏览器将发送一个对/image.png的请求,该请求不会被重定向,也不会被重写。

There are different ways how to solve that:有不同的方法可以解决这个问题:

1. Update your html/code to absolute URLs with the right path 1. 将您的 html/code 更新为具有正确路径的绝对 URL

Like this:像这样:

<img src="/es/image.png" />

That's the best solution imho.这是最好的解决方案恕我直言。 Depending on how you generate the content, there might be an option to set the base URL of the project.根据您生成内容的方式,可能会有一个选项来设置项目的基础 URL。

1b. 1b。 Make the webserver do these substitution on the fly for you让网络服务器为您即时执行这些替换

You could think about whether you want apache to make these substitutions for you, eg using mod_substitute , but there might be cases you cannot catch with this approach.您可以考虑是否希望 apache 为您进行这些替换,例如使用mod_substitute ,但在某些情况下您可能无法使用这种方法。 I would not recommend it.我不会推荐它。

2. Update your html/code to relative URLs 2. 将您的 html/code 更新为相对 URL

Ie IE

<img src="image.png" />

This sounds nice on first sight, but can become ugly if you're having complex page structure, eg /es/foo/bar/somepage.html would need <img src="../../image.png" /> to reference /es/image.png .乍一看这听起来不错,但如果您的页面结构复杂,可能会变得很难看,例如/es/foo/bar/somepage.html需要<img src="../../image.png" />参考/es/image.png

It can become especially tricky if you want to serve the same content under /es/foo and /es/foo/ (without redirecting one or the other).如果您想在/es/foo/es/foo/下提供相同的内容(不重定向其中之一),这可能会变得特别棘手。 The two would need a different relative path to the image.两者需要不同的图像相对路径。

3. Rewrite requests for /image.png 3.重写/image.png的请求

In theory, you could let Apache rewrite requests for /image.png to either /es/image.png or /en/image.png depending on the accept-language header.理论上,您可以让 Apache 将对 /image.png 的请求重写为/image.png/es/image.png/en/image.png取决于接受语言 header。

I would not recommend that, as it leads to strange behavior.我不建议这样做,因为它会导致奇怪的行为。 Eg assume a user consciously decides to visit /en/ even though his language settings prefer Spanish.例如,假设用户有意识地决定访问/en/ ,即使他的语言设置更喜欢西班牙语。 The browser would then request /image.png and the server would deliver - based on the language settings - the Spanish variant of the image.然后,浏览器将请求/image.png ,服务器将根据语言设置提供图像的西班牙语变体。 While the HTML shows the English content.而 HTML 显示的是英文内容。

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

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