.htaccess 301重定向适用于http和https情况,.com和localhost



我需要在单个页面上做301重定向。如果我只想让它从www.example.com开始工作,我需要:

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

问题是,如果用户已经登录并访问ssl保护的页面,他们可能会访问http://www.example.com/urisegment1/urisegment2/urisegment3或https://www.example.com/urisegment1/urisegment2/urisegment3/。另外,我在本地设置了这个站点,所以我可以通过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>

我刚刚尝试了以下规则

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

应该保留除了最后一个URI段之外的所有内容,因此它应该与http和不同的域一起工作。

必须更新两行中的newvalue,以避免无休止的重定向。

相关内容

最新更新