Forum Moderators: phranque

Message Too Old, No Replies

Redirect problem for all pages

         

allicat275

6:21 pm on Sep 12, 2008 (gmt 0)

10+ Year Member



Recently, I moved my website (a wordpress blog) from a provider that required urls to have the index.php in it like this:

www.example.com/index.php/%year%/%postname%

Now that I moved my site, our pages do not need to have the "index.php" in it, so they look like this:

www.example.com/%year%/%postname%

The problem is that a number of our pages were linked on other websites, so people who click these links get an error. I followed directions on other threads to change my .htaccess file to look like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ [bingegamer.net...] [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

That fixed my website so that the homepage redirects, but I want ALL pages on the site to redirect, so that when someone clicks on a link that has the "index.php" in it, it takes that out.

Thanks for your help - I'm very new at this and it is probably an easy fix!

g1smd

8:16 pm on Sep 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You want to redirect like this?

index.php/%year%/%postname% ==> www.example.com/%year%/%postname% [R=301,L]

Looks easy enough.

Is the code going in .htaccess or in httpd.conf?

allicat275

8:30 pm on Sep 12, 2008 (gmt 0)

10+ Year Member



yes, that's what I want it to do, so that when some clicks on a url that looks like this:

www.example.com/index.php/2008/some-page

it redirects them automatically to:

www.example.com/2008/some-page

I already got the homepage to work, using the code I posted above, but the rest of the site isn't working (and we have thousands of pages!)

The code is going into .htaccess, I think, unless there is another place it should go!

g1smd

8:56 pm on Sep 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No guarantees. Untested. Might have over-looked something.

RewriteRule ^index\.php/([0-9]{4})/([a-z0-9-]+)$ http://www.domain.com/$1/$2 [R=301,L]

.

Assumptions:

$1 is always four digits long.
$2 can only contain letters, numbers, hyphens.
There is never a trailing slash at the end.

.

This would also work, but doesn't have to have just digits in $1, and matches absolutely anything for $2:

RewriteRule ^index\.php/([^/]+)/(.*) http://www.domain.com/$1/$2 [R=301,L]

allicat275

9:23 pm on Sep 12, 2008 (gmt 0)

10+ Year Member



Wonderful - that works. Thank you so much for your help. You don't know how much I appreciate it!

g1smd

9:27 pm on Sep 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Once the exact definition of what is required is understood, writing the code is fairly easy.

The problem comes in that most people don't actually know what they want it to do.

As you can see, I made assumptions in both examples. You need to test with expected URLs and unexpected URLs, valid URLs and non-valid URLs, and make sure that all of them behave exactly how you want them to.