2012-11-09 52 views
0

這是我第一次嘗試編寫htaccess規則。我的目標是使所有相關鏈接重定向到絕對。首先,我是測試:htaccess RewriteRule頁面沒有正確重定向

RewriteEngine On 

RewriteRule gallery\.php$ http://www.domain.com/sample/gallery.php 
RewriteRule info\.php$ http://www.domain.com/sample/gallery.php 

的第一條規則讓火狐拋出錯誤「頁面無法正常重定向」當我點擊鏈接,而第二條規則正常工作。對於未來的想法是寫一個規則一樣,

RewriteRule catchAllRelativeLinks$ http: //www.domain.com/sample/$1 

,但如果我不能做的第一個規則的工作,我不認爲我會找到如何讓真正的規則。

編輯: 避免無限循環我不能試圖通過捕捉變量來了解我是第一次還是第二次?一些想法,我試圖(和zhcon失敗):

RewriteCond %{IS_SUBREQ} false 
RewriteRule ^gallery\.php$ http://www.domain.com/gallery.php?a [R=302,L] 

RewriteCond %{THE_REQUEST} !(\?something)$ 
RewriteRule ^gallery\.php$ http://www.domain.com/gallery.php?something [R=302,L] 

或使用環境變量,

再次感謝

+0

什麼是第一個旨在匹配是它表示爲'/ gallery.php'重定向到'/樣品/ gallery.php' ?如果是這樣,它應該包括'^',如在'^ gallery \ .php $' –

+0

U可以檢出這個解釋和指南相同(:http://stackoverflow.com/a/21754308/5465790 – Sonu

回答

1

第一條規則,你必須在一個無限循環的結果。 mod_rewrite並不是絕對的絕對相對鏈接。這應該在HTML中完成。例如,打開

<a href="gallery.php">Gallery</a> 

<a href="/sample/gallery.php">Gallery</a> 

然而,第二條規則的作品,因爲它做一些事情mod_rewrite確實非常好,而這從一個網頁或網址到另一個重定向。欲瞭解更多信息,請參閱Apache documentation

+0

爲了避免無盡的循環無法捕捉IS SUBREQ或拋出並捕獲一個參數嗎?我編輯了這個問題。 – Riccardo

+0

是的,它似乎只能重定向一次。 – Geremia

0

您可以通過此鏈接查看詳細說明。

https://wiki.apache.org/httpd/RewriteHTTPToHTTPS

這爲我工作(?:

 

    RewriteEngine on 

    # Check for POST Submission | 
    # Because POST parameters aren't retained on a redirect. 
    # You can omit that line if you want to make sure that all POST submissions are secure 
    # (any unsecured POST submissions will be ignored) 
    RewriteCond %{REQUEST_METHOD} !^POST$ 

    # Forcing HTTPS 
    RewriteCond %{HTTPS} !=on [OR] 
    RewriteCond %{SERVER_PORT} 80 
    # Pages to Apply 
    RewriteCond %{REQUEST_URI} ^something_secure [OR] 
    RewriteCond %{REQUEST_URI} ^something_else_secure 
    RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]