Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

301 redirect not working with archives pages WordPress

  • SOLVED

Recently changed the permalink structure on the site
from Domain.ca/blog/YYYY/MM/Post-title/
to Domain.ca/blog/Post-title/

Using a 301 redirect to make sure the old structure gets redirected.
RewriteEngine On
RedirectMatch 301 ^/blog/([0-9]{4})/([0-9]{2})/(.+)$ http://domain.ca/blog/$3

Unfortunately this breaks the archives page 2-n. It redirects back to the main page because of the 301 redirect.
Domain.ca/blog/YYYY/MM/page/2

Looking for the 301 redirect that will keep the permalink work and keeps the archives pages working.

See what happens with: http://nordicdesign.ca/blog/2013/02/page/2/

Answers (2)

2013-06-03

Eric P. answers:

I would use a fully mod_rewrite solution. Something like this:

RewriteEngine On
RewriteCond %{REQUEST_URI} !page
RewriteRule ^/blog/([0-9]{4})/([0-9]{2})/(.+)$ http://domain.ca/blog/$3 [L,R=301]


That will onl apply the RewriteRule if the REQUEST_URI doesn't contain the string "page".


NordicDesign comments:

Hi Eric, It does fix the /page/2/ but it does not work for the permalink redirect.

Any other thoughts? Could there be something be added to the archives link? (blog/archive/2013/02/title)

Thanks!

NordicDesign


Eric P. comments:

I just tested myself on my own blog. You're correct, it didn't work for me either. This works on my site:

RewriteEngine On
RewriteCond %{REQUEST_URI} !page
RewriteRule ^blog/([0-9]{4})/([0-9]{2})/(.+)$ http://%{HTTP_HOST}/blog/$3 [L,R=301]


Seems what the RewriteRule gets does not include the initial "/" character on my server. Maybe that works for you.


Eric P. comments:

Slight improvement on that last answer.

RewriteEngine On
RewriteCond %{REQUEST_URI} !page
RewriteRule ^/*blog/+[0-9]{4}/+[0-9]{2}/+(.+)$ http://%{HTTP_HOST}/blog/$1 [L,R=301]


This removes unneeded parentheses, and it handles any number of "stutter slashes" at the beginning or in the middle of the URL.

It will work whether the leading slash is there or not.


NordicDesign comments:

Hi Eric,

It seems to be working nicely with the archives pages, though it does not redirect the permalink change.....

Is there maybe a way to exclude the /page/2/ from the redirect?

KO


Eric P. comments:

Be sure to clear your cache after any changes to the .htaccess file. Browsers tend to cache those redirects, and you might not see the results. Alternatively, use a different (wrong) month in the URL, one you've never used before and see if it removes that.

If it's still not working with my latest suggestion, I sent you a PM with contact info. I'll be happy to check your full .htaccess file and look at your logs and see what's happening on your server.


Eric P. comments:

P.S. Right now, when I go to [[LINK href="http://nordicdesign.ca/blog/page/2/"]]http://nordicdesign.ca/blog/page/2/[[/LINK]], it shows the archive properly, and when I go to [[LINK href="http://nordicdesign.ca/blog/2011/07/cafe-laundromat/"]]http://nordicdesign.ca/blog/2011/07/cafe-laundromat/[[/LINK]], it's redirecting (with a "301
Moved Permanently" header) to [[LINK href="http://nordicdesign.ca/blog/cafe-laundromat/"]]http://nordicdesign.ca/blog/cafe-laundromat/[[/LINK]]

From my end, it's working now with what you have. I think your browser has some cached broken redirects from earlier.


NordicDesign comments:

Hi Eric,

The problem is with:
http://nordicdesign.ca/blog/2013/03/page/2/ (<- needs to stay this way - Broken)
http://nordicdesign.ca/blog/2011/07/cafe-laundromat/ (<- requires redirect - Works)

I unfortunately cant give you access at this moment. I did test with a clean browser with same results.

Let me know if you have more ideas!

KO


Eric P. comments:

For a while, it was actually working the way you wanted.

Can you put my last suggestion in, at the top of your .htaccess? I gave you other contact info by pm.


Eric P. comments:

Did you resolve this.

I'm 100% certain that the last two RewriteCond/RewriteRule combinations work on several servers, and they should work on yours.

I've also noted that some browsers still keep the 301 redirects in some kind of memory even after clearing the cache. So i you try these, make sure you go to new months (try 25 in the month part of the URL for the posts, but for archives, go to a month you've never been to).

On my machine, Chrome keeps the 301 redirects after clearing the cache. You have to clear cache, then completely quite Chrome and wait for all processes to end before starting it again.


Eric P. comments:

OK. One more suggestion. I just installed a test site for this, and here's what I see.

First, the Fantastico WordPress installer on my hosting service generated this in .htaccess.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# END WordPress


To that, I added these two lines, right after the "RewriteBase /blog/" line:

RewriteCond %{REQUEST_URI} !page
RewriteRule ^[0-9]{4}\/+[0-9]{2}\/+(.+)$ /blog/$1 [L,R=301]


The results are here:

[[LINK href="http://247.mrmfl.net/blog/2013/03/a-post-from-early-march/"]]http://247.mrmfl.net/blog/2013/03/a-post-from-early-march/[[/LINK]] (redirects with 301)

[[LINK href="http://247.mrmfl.net/blog/2013/03/"]]http://247.mrmfl.net/blog/2013/03/[[/LINK]] (does not redirect, correct)

[[LINK href="http://247.mrmfl.net/blog/2013/03/page/2/"]]http://247.mrmfl.net/blog/2013/03/page/2/[[/LINK]] (does not redirect, correct).

This definitely works. If it doesn't work on your server, I sent you info to contact me. I don't need access, but I do need to know the full content of your .htaccess file now, when it's not working.


Eric P. comments:

To be clear, after the addition, here's the full content of my working .htaccess file.

<blockquote>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
<strong>RewriteCond %{REQUEST_URI} !page
RewriteRule ^[0-9]{4}\/+[0-9]{2}\/+(.+)$ /blog/$1 [L,R=301]
</strong>RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# END WordPress
</blockquote>


NordicDesign comments:

Eric,

You really went all out on this one! :-)

Congratulations, you made it work. If we have some more complex work, ill make sure to contact you.

Great work!!

NordicDesign.ca

2013-06-02

Yakir Sitbon answers:

Try this:
RedirectMatch 301 ^/blog/([0-9]{4})/([0-9]{2})/(.*)$ http://domain.ca/blog/$3


NordicDesign comments:

Sorry, Yakir, this made it worst. With this configuration the archive (Domain.ca/blog/YYYY/MM/) pages do not show at all anymore.