2017-02-23 47 views
0

如果文件或文件夾不存在,我有以下配置僅可用於index.html,以便與AngularJS一起使用。Apache根據REQUEST_URI提供不同的index.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 
    RewriteRule^/index.html 

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

我想什麼做的是: 檢查REQUEST_URI如果與像茹郎前綴開始| ES |克,在前面加上這2個字母index.html

所以,如果訪問example.com Apache將index.html的服務從當前的根,但如果我訪問example.com/ru Apache將成爲index.htmlDOCUMENT_ROOT + RU文件夾/ru/index.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> 

但似乎沒有工作。

+0

我想默認情況下,如果沒有指定文件名,Web服務器將從各自的文件夾提供'index.html'。嘗試刪除規則? – Icycool

+0

@Icycool嗨,當訪問'example/ru'時,它會正確地提供'index.html',但是當訪問'example/ru/somepage/subpage /'時,它將從根目錄服務'index.html',因爲這個文件夾不會不存在,但它以'ru'開始,所以我需要'/ ru/index.html'來提供服務。 – Froxz

+0

如何在前面添加斜線? 'RewriteRule ^/$ 1/index.html' – Icycool

回答

2

您正在使用$ 1,但這種類型的變量引用僅適用於拍攝組從重寫規則不從的RewriteCond,你捕捉沒什麼重寫規則,正確的事情是做1個單指令:

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

或者如果你堅持使用,否則不需要的RewriteCond,使用正確的捕捉變量,注意捕捉組來自rewriteconds使用%符號來代替$:

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

注意:您可以在這個缺少一個L標誌一。

+0

謝謝你這個工作'RewriteRule^/(ru | es | gr)//$ 1/index.html'非常簡單,謝謝。 – Froxz

相關問題