2013-01-16 60 views
0

我需要在單個頁面上執行301重定向。如果我只是想讓它從www.example.com工作,我會:.htaccess 301重定向適用於http和https個案,.com和本地主機

redirect 301 /urisegment1/urisegment2/urisegment3 http://www.example.com/urisegment1/urisegment2/NEWURISEGMENT3 

的問題是,用戶可以訪問http://www.example.com/urisegment1/urisegment2/urisegment3或有時https://www.example.com/urisegment1/urisegment2/urisegment3/如果他們已經在和訪問SSL保護的頁面被記錄。此外,我也在本地設置了該網站,因此我可以通過它訪問它http://www.example.local/urisegment1/urisegment2/urisegment3

如何獲得此功能以適用於所有情況?


(這是問題的結束,在以下情況下,有關我目前的.htaccess文件,注意它主要不是我自己的工作,這是從各種渠道獲得)。

<IfModule mod_rewrite.c> 

RewriteEngine on 
RewriteBase/


#Removes trailing slashes (prevents SEO duplicate content issues) 
#RewriteCond %{REQUEST_FILENAME} !-d 
#RewriteRule ^(.+)/$ $1 [L,R=301] 

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

#force homepage to http from https 
RewriteCond %{HTTP_HOST} ^www\.example\.com$ 
RewriteCond %{HTTPS} on 
RewriteRule ^$ http://www.example.com/ [R=301,L] 

#If a controller can't be found - then issue a 404 error from PHP 
ErrorDocument 404 /404.html 

#Removes access to the system folder by users. 
#Additionally this will allow you to create a System.php controller, 
#previously this would not have been possible. 
#'system' can be replaced if you have renamed your system folder. 
RewriteCond %{REQUEST_URI} ^sys.* 
RewriteRule ^(.*)$ /index.php/$1 [L] 

# Checks to see if the user is attempting to access a valid file, 
# such as an image or css document, if this isn't true it sends the 
# request to index.php. The first condition allows access to assets, css, js and the robots file 
RewriteCond $1 !^(index\.php|assets|css|js|robots\.txt) 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?/$1 [L] 


RewriteCond %{THE_REQUEST} ^.*/index.php 
RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L] 

# Removes access to the system folder by users. 
# Additionally this will allow you to create a System.php controller, 
# previously this would not have been possible. 
# 'system' can be replaced if you have renamed your system folder. 
RewriteCond %{REQUEST_URI} ^system.* 
RewriteRule ^(.*)$ /index.php/$1 [L] 


</IfModule> 


<IfModule !mod_rewrite.c> 

# Without mod_rewrite, route 404's to the front controller 
ErrorDocument 404 /index.php 

</IfModule> 

回答

0

我只是想下面的規則

RewriteCond %{REQUEST_URI} !newvalue/?$ 
RewriteRule ^(.*)/[^/]+/?$ /$1/newvalue [L,R=301] 

應該保持一切,但最後的URI段,所以應該用HTTP(S)和不同領域的工作。

您必須在兩行中更新新值以避免無盡的重定向。

+0

感謝您的回覆。然而,讓我們說我的'urisegment3'就像'example-directory-about-computers',而我的新'urisegment3'是'computer-directory',那麼重寫規則會如何從舊的重定向到新的? 'newvalue'在兩條線上?我很遺憾地嘗試過它 – whispersan