0

I need to do a 301 redirect on a single page. If I just wanted it to work from www.example.com, I would have:

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

The problem is, users may access http://www.example.com/urisegment1/urisegment2/urisegment3 or sometimes https://www.example.com/urisegment1/urisegment2/urisegment3/ if they have been logged in and access SSL-protected pages. Also I have the site set up locally so I can access it through http://www.example.local/urisegment1/urisegment2/urisegment3

How can I get this to work for all cases?


(That's the end of the question, my current .htaccess file below in case it is relevant. Note it's mostly not my own work, it is drawn from various sources).

<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>
whispersan
  • 1,029
  • 2
  • 13
  • 28

1 Answers1

0

I just tried the following rule

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

which should keep everything but the last URI segment, so it should work with http(s) and different domains.

You have to update the newvalue in both lines to avoid endless redirects.

Dehalion
  • 757
  • 1
  • 7
  • 16
  • Thanks for the reply. However let's say my 'urisegment3' was something like 'example-directory-about-computers' and my new 'urisegment3' was 'computer-directory', how would the rewrite rule you posted redirect from old to new? 'newvalue' is on both lines? I tried it without success unfortunately – whispersan Jan 17 '13 at 01:09