Forum Moderators: phranque

Message Too Old, No Replies

How to do 301 Redirect for .htaccess file already using rules

Already using rule to remove index.php from URL, usual redirects don't work

         

stemc9

2:30 pm on Jan 12, 2008 (gmt 0)

10+ Year Member



Hi there,

I’ve just implemented a new content management system (ExpressionEngine) for my website and this means that the old php URL's have now changed, for example, mywebsite.com/gallery.php is now mywebsite.com/gallery/

The CMS I'm using inserts index.php into the URL's by default, for example, mywebsite.com/index.php/gallery/, so I already have three lines in my .htaccess file to remove this from the URL structure.

But now that I want to insert some 301 redirects, I'm finding that the usual pieces of code that I've found on the web simply don't work, I suspect, due to them conflicting with the code I'm already using to remove index.php from the URL.

Here’s the existing .htaccess file for getting rid of index.php from the URL:


RewriteEngine on
RewriteCond $1 ^(about¦news¦gallery¦services¦contact¦tags¦image¦P[0-9]{2,8}) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]

I tried a few different methods of 301 redirecting (including using php in a file), but the only one that almost works is when the 4th line below is added to the .htaccess file:


RewriteEngine on
RewriteCond $1 ^(about¦news¦gallery¦services¦contact¦tags¦image¦P[0-9]{2,8}) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]
Redirect 301 /gallery.php http://www.mywebsite.com/gallery/

This redirects to the correct page (the others just displayed the home page), but the URL was slightly wrong - it was showing [mywebsite.com...] instead of [mywebsite.com...]

I'm completely stuck on this now so would appreciate any expert advice on how I can get these 301 redirects working properly (given the first 3 lines that I need for my CMS as explained above).

Thanks,

Ste

jdMorgan

5:06 pm on Jan 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So to be clear, you want to redirect to add a trailing slash to requests for "/gallery", and the following method will not work?

RewriteEngine on
#
RewriteRule ^gallery\.php$ http://www.example.com/gallery/ [R=301,L]
#
RewriteCond $1 ^(about¦news¦gallery¦services¦contact¦tags¦image¦P[0-9]{2,8}) [NC]
RewriteRule (.*) /index.php?/$1 [L]

As another point of confusion, why do you have a "?" in the middle of the "/index.php?/$1" substitution URL-path in the (now-second) rule above?

Note that posting on this forum changes the pipe character to a broken pipe "¦" character. Replace the broken pipes with solid pipes before trying to use any code posted here.

Jim

stemc9

11:57 pm on Jan 12, 2008 (gmt 0)

10+ Year Member



Hi Jim,

I've just copied and pasted your rules (and replacing the half-pipe characters), and it worked perfectly, just as I wanted, straight away! :)

Thank you so much giving me the solution to this!

That question mark after the index.php is required by default by my CMS - there is an option in the control panel to 'Force URL query strings' which should mean that it's not needed. For some reason, if I remove the question mark, none of my pages load other than the home page, and all the other pages load the home page (the home page loading usually means the page wasn't found).

Thanks again,

Ste