Apache基于REQUEST_URI提供不同的index.html

时间:2017-02-23 10:00:45

标签: html angularjs apache

如果文件或文件夹不存在,我有以下配置仅用于index.html,以便与AngularJS一起使用。

<VirtualHost 192.168.0.1:80>
    DocumentRoot /path/to/my/folder

    RewriteEngine On  
    # If an existing asset or directory is requested go to it as it is
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
    RewriteRule ^ - [L]

    # If the requested resource doesn't exist, use index.html
    RewriteRule ^ /index.html

    ErrorLog "/var/log/apache2/angular.com-error.log"
    CustomLog "/var/log/apache2/angular.com-access.log" common
</VirtualHost>

我想做的是: 检查REQUEST_URI是否以ru | es | gr之类的lang前缀开头,将这2个字母添加到index.html

因此,如果访问example.com apache将从当前root用户提供index.html但如果我访问example.com/ru apache将从index.html + ru文件夹/ ru / index提供DOCUMENT_ROOT html的

我做过这样的事情:

<VirtualHost 192.168.0.1:80>
    DocumentRoot /path/to/my/folder

    RewriteEngine On  
    # If an existing asset or directory is requested go to it as it is
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
    RewriteRule ^ - [L]

    # If the requested resource doesn't exist, use index.html
    RewriteCond %{REQUEST_URI} ^/(ru|es|gr)/
    RewriteRule ^ $1/index.html

    ErrorLog "/var/log/apache2/angular.com-error.log"
    CustomLog "/var/log/apache2/angular.com-access.log" common
</VirtualHost>

但似乎没有用。

1 个答案:

答案 0 :(得分:2)

你使用的是$ 1,但是这种类型的变量引用仅适用于来自rewriterule而不是来自rewritecond的捕获组,并且你在rewriterule中没有捕获任何内容,正确的做法是在1个单指令中执行它:

RewriteRule ^/(ru|es|gr)/ /$1/index.html

如果您坚持使用其他不需要的重写,使用正确的捕获变量,请注意从重写中捕获的组使用%符号而不是$:

RewriteCond %{REQUEST_URI} ^/(ru|es|gr)/
RewriteRule ^ /%1/index.html

注意:您可能在此错过了一个L标志。

相关问题