0

我需要在单个页面上进行 301 重定向。如果我只是想让它在 www.example.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>
4

1 回答 1

0

我刚刚尝试了以下规则

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

它应该保留除最后一个 URI 段之外的所有内容,因此它应该适用于 http(s) 和不同的域。

您必须更新行中的新值以避免无休止的重定向。

于 2013-01-16T23:15:58.543 回答