2012-10-19 83 views
1

我有一個網站brosing一些網頁時觸發HTTPS,我想恢復到HTTP不瀏覽這些網頁的時候,這是我的htaccess:Apache的mod_rewrite的規則,禁用HTTPS/SSL除了一些網頁

<IfModule mod_rewrite.c> 

    RewriteEngine on 
    #RewriteBase /myproject/ 

    # toggle www prefix 
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 

    # redirect favicon requests to the correct favicon.ico 
    RewriteCond %{REQUEST_URI} !^/favicon\.ico [NC] 
    RewriteCond %{THE_REQUEST} favicon\.(ico|png|gif|jpe?g) [NC] 
    RewriteRule (.*) http://mysite/favicon.ico [R=301,L] 

    # hide index.php from root 
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC] 
    RewriteRule^/%1 [R=301,L] 

    # disable HTTPS when not needed 
    RewriteCond %{HTTPS} on 
    #RewriteCond %{HTTP_REFERER} !^(https)(.*)$ 
    RewriteCond %{REQUEST_URI} !^(.*)/(exception|xyz|pqr)(.*)$ [NC] 
    RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

    # if a directory or a file exists, use it directly 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 

    # otherwise forward it to index.php 
    RewriteRule . index.php [L,QSA] 
    #RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA] 

</IfModule> 

如果我打電話https://host/url重定向工作正常着陸http://host/url,但如果我叫https://host/exception,而不是把它當作是我會被重定向到http://host/index.php

什麼是錯的?由於


更新與解決方案,希望這將是一個人

<IfModule mod_rewrite.c> 

    RewriteEngine on 
    #RewriteBase /yourbase 

    # hide index.php from root 
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC] 
    RewriteRule^/%1 [R=301,L] 

    # pass-through so when the rules loop (https), the redirect to index won't get applied 
    RewriteRule index.php$ - [L] 

    # toggle www prefix 
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 

    # redirect favicon requests to the correct favicon.ico 
    RewriteCond %{REQUEST_URI} !^/favicon\.ico [NC] 
    RewriteCond %{THE_REQUEST} favicon\.(ico|png|gif|jpe?g) [NC] 
    RewriteRule (.*) http://liberos.it/favicon.ico [R=301,L] 

    # disable HTTPS when not needed (but don't rewrite direct files requests) 
    RewriteCond %{HTTPS} on 
    #RewriteCond %{HTTP_REFERER} !^(https)(.*)$ 
    RewriteCond %{REQUEST_URI} !^(.*)/(yourpath|another/path)(.*)$ [NC] 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d [OR] 
    RewriteCond %{REQUEST_URI} . 
    RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

    # if a directory or a file exists use it directly, otherwise forward it to index.php 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule . index.php 
    #RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA] 

</IfModule> 
+0

可能重複[301的.htaccess重定向所有HTTPS訪問http除了一個PAGE](http://stackoverflow.com/questions/2079015/htaccess-301-redirect-for-all-https-http-except-one-page) – jww

回答

0

既然你是通過index.php路由的一切,你需要通過傳遞來創建一個規則循環,所以當有用的,重定向將不會被應用。試着在你的規則列表的最頂端添加此規則:

RewriteRule index.php$ - [L] 
+0

謝謝,這個工作!但現在我必須解決下一個問題:該頁面沒有完整的https內容,因此瀏覽器顯示不安全內容的警告。 我可以通過啓用此註釋行來解決此問題 #RewriteCond%{HTTP_REFERER}!^(https)(。*)$ 但使用引用者並不好,因爲當我單擊一個應該使用http的https鏈接時,它將不匹配該規則並將保持https直到我點擊另一個鏈接,然後將切換到http –

+0

請注意,我也必須在你的建議規則之前放置從根目錄隱藏index.php的部分,否則'https://主機/索引。 php'將可到達,我不想 –

+0

我也解決了這個問題,我發佈我的最終htaccess希望將有人幫助 –

0
<IfModule mod_rewrite.c> 

    RewriteEngine on 
    #RewriteBase /yourbase 

    # hide index.php from root 
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC] 
    RewriteRule^/%1 [R=301,L] 

    # pass-through so when the rules loop (https), the redirect to index won't get applied 
    RewriteRule index.php$ - [L] 

    # toggle www prefix 
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 

    # redirect favicon requests to the correct favicon.ico 
    RewriteCond %{REQUEST_URI} !^/favicon\.ico [NC] 
    RewriteCond %{THE_REQUEST} favicon\.(ico|png|gif|jpe?g) [NC] 
    RewriteRule (.*) http://liberos.it/favicon.ico [R=301,L] 

    # disable HTTPS when not needed (but don't rewrite direct files requests) 
    RewriteCond %{HTTPS} on 
    #RewriteCond %{HTTP_REFERER} !^(https)(.*)$ 
    RewriteCond %{REQUEST_URI} !^(.*)/(yourpath|another/path)(.*)$ [NC] 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d [OR] 
    RewriteCond %{REQUEST_URI} . 
    RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

    # if a directory or a file exists use it directly, otherwise forward it to index.php 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule . index.php 
    #RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA] 

</IfModule>