2012-04-14 84 views
1

我試圖使用mod_rewrite重定向某些頁面以使用SSL。對於我有:帶外部重定向和內部重寫的mod_rewrite

RewriteCond %{SERVER_PORT} ^443$ 
RewriteCond %{REQUEST_URI} !^/login(\.php)?$ [NC] 
RewriteCond %{REQUEST_URI} !^/contact-us(\.php)?$ [NC] 
RewriteCond %{REQUEST_URI} !^/\..*$ 
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] 

RewriteCond %{HTTP_HOST} !^dev\.example\.com$ [NC] 
RewriteCond %{SERVER_PORT} ^80$ 
RewriteCond %{REQUEST_URI} ^/login(\.php)?$ [NC,OR] 
RewriteCond %{REQUEST_URI} ^/contact-us(\.php)?$ [NC] 
RewriteRule ^(.+)\.php$ https://www.example.com/$1 [R=301,L] 

這工作正常,並且正是我想要它做。

在我.htacess後來我有一個:

RewriteRule ^members/(.+)/change-password$ members/.change-password.php?item=$1 [NC,QSA,L] 

因此,如果出現一個網址,例如:

/members/.change-password.php?item=foo-bar 

http://www.example.com/members/foo-bar/change-password 

內部將它作爲處理

再次,這工作得很好,正在做我想要的東西。

我現在需要做的是將其包含在我的原始SSL重定向邏輯中,以確保任何更改密碼請求都被重定向到相同的URL,而不是通過https。我試過了:

RewriteCond %{SERVER_PORT} ^443$ 
RewriteCond %{REQUEST_URI} !^/login(\.php)?$ [NC] 
RewriteCond %{REQUEST_URI} !^/contact-us(\.php)?$ [NC] 
RewriteCond %{REQUEST_URI} !^/\..*$ 
RewriteCond %{REQUEST_URI} !^/members/.+/change-password [NC] 
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] 

RewriteCond %{HTTP_HOST} !^dev\.example\.com$ [NC] 
RewriteCond %{SERVER_PORT} ^80$ 
RewriteCond %{REQUEST_URI} ^/login(\.php)?$ [NC,OR] 
RewriteCond %{REQUEST_URI} ^/contact-us(\.php)?$ [NC,OR] 
RewriteCond %{REQUEST_URI} ^/members/.+/change-password [NC] 
RewriteRule ^(.+)\.php$ https://www.example.com/$1 [R=301,L] 

但這不起作用 - 我只是通過http傳遞頁面。將.+更改爲.*似乎讓我進入永久性重定向循環。

我猜這是因爲內部重寫,但無論我嘗試什麼,我似乎無法解決它。

任何人都可以請指教?

感謝,

亞當M.

+0

不要依靠自動重定向,請確保您的鏈接指向這些部分使用「https://」。 (有關詳細信息,請參見[本答案](http://webmasters.stackexchange.com/a/28443/11628)和[本答案](http://stackoverflow.com/a/9105894/372643)。) – Bruno 2012-04-14 18:21:12

+0

@布魯諾感謝您的迴應,但前端鏈接直接指定爲https。自動重定向的原因是對於那些自己輸入url的人來說是帶和大括號。雖然我可以''非]請求的網址,不會提供我所期望的用戶體驗(儘管我當然不是每個人都會同意這種做法)。 – 2012-04-14 19:44:34

+0

只要您在開發應用程序時不假定這些重定向在那裏,您的方法的確有意義。 – Bruno 2012-04-15 17:38:55

回答

3

mod_rewrite的文檔的進一步審查導致我有點我錯過了特定的.htaccess文件的使用。基本上[L]標誌實際上並不表示最後的標準。相反,您需要使用[END]標誌(引用http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l)。

當然,這導致了我的另一個問題 - 我的託管服務提供商沒有Apache或mod_rewrite的最新安裝,因此[END]標誌觸發了無法訪問的HTTP 500內部服務器錯誤。

那該怎麼辦?嗯,我又回到了我原來的規則集與知識[L]沒有做什麼,我期待和發現錯誤馬上 - 在%{REQUEST_URI}值已被內部重寫更新:

RewriteRule ^members/(.+)/change-password$ members/.change-password.php?url-slug=$1 [NC,QSA,L] 

因此改變我原來的重定向邏輯排除這個解決我的問題:

RewriteCond %{SERVER_PORT} ^443$ 
RewriteCond %{REQUEST_URI} !^/login(\.php)?$ [NC] 
RewriteCond %{REQUEST_URI} !^/contact-us(\.php)?$ [NC] 
RewriteCond %{REQUEST_URI} !^/\..*$ 
RewriteCond %{REQUEST_URI} !^/members/.+/change-password$ [NC] 

RewriteCond %{REQUEST_URI} !^/members/\.change-password(\.php)? [NC]

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] 

RewriteCond %{HTTP_HOST} !^dev\.example\.com$ [NC] 
RewriteCond %{SERVER_PORT} ^80$ 
RewriteCond %{REQUEST_URI} ^/login(\.php)?$ [NC,OR] 
RewriteCond %{REQUEST_URI} ^/contact-us(\.php)?$ [NC,OR] 
RewriteCond %{REQUEST_URI} ^/members/.+/change-password$ [NC] 
RewriteRule ^(.+)(\.php)?$ https://www.example.com/$1 [R=301,L]