logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$5
301 redirect not working with archives pages

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/

This question has been answered.

NordicDesign | 06/02/13 at 3:29pm Edit
Tutorial: How to assign prize money


(15) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    06/02/13
    4:46pm
    Yakir Sitbon says:

    Try this:

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

  • avatar
    Last edited:
    06/02/13
    11:20pm
    Eric P. says:

    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".

  • avatar
    Last edited:
    06/02/13
    6:06pm
    NordicDesign says:

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

  • avatar
    Last edited:
    06/03/13
    12:10am
    NordicDesign says:

    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

  • avatar
    Last edited:
    06/03/13
    4:17pm
    Eric P. says:

    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.

  • avatar
    Last edited:
    06/03/13
    4:29pm
    Eric P. says:

    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.

  • avatar
    Last edited:
    06/03/13
    6:20pm
    NordicDesign says:

    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

  • avatar
    Last edited:
    06/03/13
    6:37pm
    Eric P. says:

    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.

  • avatar
    Last edited:
    06/03/13
    6:45pm
    Eric P. says:

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

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

  • avatar
    Last edited:
    06/03/13
    7:16pm
    NordicDesign says:

    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

  • avatar
    Last edited:
    06/03/13
    7:58pm
    Eric P. says:

    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.

  • avatar
    Last edited:
    06/04/13
    1:36pm
    Eric P. says:

    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.

  • avatar
    Last edited:
    06/06/13
    6:23pm
    Eric P. says:

    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:

    http://247.mrmfl.net/blog/2013/03/a-post-from-early-march/ (redirects with 301)

    http://247.mrmfl.net/blog/2013/03/ (does not redirect, correct)

    http://247.mrmfl.net/blog/2013/03/page/2/ (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.

  • avatar
    Last edited:
    06/06/13
    6:30pm
    Eric P. says:

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


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

    # END WordPress

  • avatar
    Last edited:
    06/08/13
    4:23pm
    NordicDesign says:

    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

This question has expired.



NordicDesign, Arnav Joy voted on this question.



Current status of this question: Completed



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.