Forum Moderators: phranque
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
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]
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
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