WordPress.org

Ready to get started?Download WordPress

Forums

How to set .htaccess correctly for 301 redirects to subdirectory. (13 posts)

  1. alexbowyer
    Member
    Posted 8 months ago #

    Hi, I have an unusual WordPress migration / htaccess redirect situation I need some help with.

    I had an existing personal blog at the root of alexbowyer.com.
    I want to set up a new wordpress site for my business at the root of alexbowyer.com, and migrate the existing personal blog from alexbowyer.com to blog.alexbowyer.com.

    I have successfully migrated the blog to blog.alexbowyer.com, but now I am trying to set up the 301 redirects correctly, so that I don't lose SEO juice/google links etc to my old personal blog.

    I can't redirect all alexbowyer.com traffic because I want to set up a new WP site at that address. So I have added specific .htaccess redirects at the root of alexbowyer.com for every post like this:

    Redirect 301 /2013/07/fantasia-festival-2013-mini-reviews/ http://blog.alexbowyer.com/2013/07/fantasia-festival-2013-mini-reviews/

    The problem I have is that I am getting "Too many redirects" when trying to view posts in the 'blog' subdirectory - presumably because the htaccess file from the parent/root directory is being picked up, creating a never ending loop.

    I need some way to make the redirect only have effect on requests made to alexbowyer.com ROOT but not requests made to blog.alexbowyer.com.

    I hope someone can help?

  2. alexbowyer
    Member
    Posted 8 months ago #

    Just to clarify, the problem occurs both when using one of the URLs from the original domain e.g. http://alexbowyer.com/2013/07/fantasia-festival-2013-mini-reviews/
    as well as when using new URL like http://blog.alexbowyer.com/2013/07/fantasia-festival-2013-mini-reviews/ and goes away (for blog.alexbowyer.com link but not for alexbowyer.com) if I remove the Redirect line for that post, so I'm certain it's all down to the .htaccess file.

  3. Pioneer Valley Web Design
    Member
    Posted 8 months ago #

    Try:

    RedirectMatch 301 (?i)http://alexbowyer.com/2013/07/fantasia-festival-2013-mini-reviews/ http://blog.alexbowyer.com/2013/07/fantasia-festival-2013-mini-reviews/

    (the (?i) means the url case sensitivity is ignored) and we include the full path in the first part also.

  4. alexbowyer
    Member
    Posted 8 months ago #

    Thanks for the suggestion, I just tried it (it's in place now) but

    http://alexbowyer.com/2013/07/fantasia-festival-2013-mini-reviews/ now yields a 404 :-(

  5. Pioneer Valley Web Design
    Member
    Posted 8 months ago #

    Please explain what you are trying to do again.

    Do you want to content at blog.yoursite.com/whatever/ to show a url of yousite.com/whatever/ ?

    Also, have you placed the rules above any other rules in the root .htaccess?

    and, the whole site is returning a 404. Probably due to this?
    =====> //http://suspended.hostgator.com/js/simple-expand.min.js
    please discuss with host.

  6. alexbowyer
    Member
    Posted 8 months ago #

    No, I want a request to (for example)
    http://alexbowyer.com/2013/07/fantasia-festival-2013-mini-reviews/

    to be 301 redirected to
    http://blog.alexbowyer.com/2013/07/fantasia-festival-2013-mini-reviews/

    where the blog has been migrated to.

    On the server, the alexbowyer.com site is served up from /public_html/ and the blog.alexbowyer.com is served up from /public_html/blog/

    I have only one .htaccess, in the /public_html/ directory. The 404s only happen when I start the redirect match condition's first address with http://alexbowyer.com ... doesn't happen when redirect is set to match a relative path e.g. /2013/07/fantasia-festival-2013-mini-reviews/ (I have set it back to this now so you can see the problem).

    HOWEVER this breaks the ability of http://blog.alexbowyer.com/2013/07/fantasia-festival-2013-mini-reviews/ (the redirect destination) to be resolvable (presumably because the .htaccess in the parent directory is still picked up, causing the "too many redirects message" you see now I have put the file back as it was). Hope that is clearer!

    Oh and the root alexbowyer.com 404 is explained because I have Options -Indexes in place and no index.php file.

    Thanks for spending time helping me on this - I am stumped!!

  7. Pioneer Valley Web Design
    Member
    Posted 8 months ago #

    Please discuss with host or please find another means of support. These issues are host related. You cannot redirect from a site that does not exist or is not functioning.

  8. alexbowyer
    Member
    Posted 8 months ago #

    I think you are saying the problem is that the .htaccess file is ignored when there is no index.php file to load.

    To prove this is not the issue, I have installed a simple index.php file which loads phpinfo.

    You can see all of the following URLs load fine (in other words both destination site and "old" site do indeed exist):

    You can see that none of the following URLs correctly load content on blog.alexbowyer.com:

    It's not a hosting problem nor a problem with a non-existent site, it's a redirection problem.

    Currently I have the following .htaccess rules in place (this is the entirety of the file):

    Redirect 301 /2013/07/fantasia-festival-2013-mini-reviews/ http://blog.alexbowyer.com/2013/07/fantasia-festival-2013-mini-reviews/
    RedirectMatch 301 (?i)http://alexbowyer.com/2013/03/canadian-mobile-phone-charges-5-important-questions-that-deserve-an-answer/ http://blog.alexbowyer.com/2013/03/canadian-mobile-phone-charges-5-important-questions-that-deserve-an-answer/
    Redirect 301 http://alexbowyer.com/2013/01/science-friction-review/ http://blog.alexbowyer.com/2013/01/science-friction-review/
    Options -Indexes

    I am sorry if my previous response was not clear enough or frustrated you somehow, but this is a genuine problem and I am struggling to find the solution online.

    Any help from you or others would be appreciated.
    Thanks.

  9. Pioneer Valley Web Design
    Member
    Posted 8 months ago #

    It's not a hosting problem nor a problem with a non-existent site, it's a redirection problem.

    It was when I wrote above...Now we have this:

    To prove this is not the issue, I have installed a simple index.php file which loads phpinfo.

    That only proves that PHP is running on the server.

    Currently I have the following .htaccess rules in place

    The links at blog. work, any attempts to redirect from site.com do not, then, again, you need to ask your host about this...the redirects should work. If the host does not have a site linked to the main domain, then a redirect.

    404 not found: http://validator.w3.org/check?uri=http%3A%2F%2Falexbowyer.com%2F2013%2F03%2Fcanadian-mobile-phone-charges-5-important-questions-that-deserve-an-answer%2F&charset=%28detect+automatically%29&doctype=Inline&group=0&ss=1&outline=1

    301 moved permanently: http://validator.w3.org/check?uri=http%3A%2F%2Falexbowyer.com%2F2013%2F07%2Ffantasia-festival-2013-mini-reviews%2F&charset=%28detect+automatically%29&doctype=Inline&group=0&ss=1&outline=1

    this is happening as you are using RedirectMatch here and that was the url being browsed to, so you got a redirect loop - why? because the url does not exist to start with - remove that rule and it will revert to a 404

    404 not found: http://validator.w3.org/check?uri=http%3A%2F%2Falexbowyer.com%2F2013%2F01%2Fscience-friction-review%2F&charset=%28detect+automatically%29&doctype=Inline&group=0&ss=1&outline=1

    The issue is that the main site is returning 404 to start with.

  10. alexbowyer
    Member
    Posted 8 months ago #

    Thank you for your response and continuing to engage me on this.

    We'll have to agree to disagree about the 404 situation at the root URL. It's irrelevant to the issue I am trying to fix anyway:

    I think there is something from my last post that I did not clearly convey.

    The reason I have left my .htaccess file with only 3 (different) redirect rules is to show both what I had, and your suggested solution, and a third solution, side by side, all still active and inspectable by you.

    Let's consider each blog post in turn, hopefully you will then see what my last post was trying to convey:

    Blog post 1
    Slug: fantasia-festival-2013-mini-reviews
    Blog post exists at http://blog.alexbowyer.com (load the base URL, hover over first post title, look at the URL in the status bar).
    Redirect rule in effect:

    Redirect 301 /2013/07/fantasia-festival-2013-mini-reviews/ http://blog.alexbowyer.com/2013/07/fantasia-festival-2013-mini-reviews/

    Result: Neither the blog.alexbowyer.com URL for this post, nor the alexbowyer.com URL for this post work. Both get "too many redirects" warning.
    My interpretation: the redirect rule is being picked up a second time, because blog.alexbowyer.com exists in a subdirectory underneath the root of alexbowyer.com (where the .htaccess lives)

    Blog post 1 illustrates the initial problem I reported.

    Blog post 2
    Slug: canadian-mobile-phone-charges-5-important-questions-that-deserve-an-answer
    Blog post exists at http://blog.alexbowyer.com/2013/03/canadian-mobile-phone-charges-5-important-questions-that-deserve-an-answer/ and loads fine via that URL
    Redirect rule in effect (your suggestion):

    RedirectMatch 301 (?i)http://alexbowyer.com/2013/03/canadian-mobile-phone-charges-5-important-questions-that-deserve-an-answer/ http://blog.alexbowyer.com/2013/03/canadian-mobile-phone-charges-5-important-questions-that-deserve-an-answer/

    Result: When accessed via alexbowyer.com URL we get a 404.
    My interpretation: the redirectMatch rule is failing to find a match, so the URL behaves exactly the same as if nothing exists at that URL.

    Blog post 2 illustrates your suggested fix, which so far does not work for me. Note that the blog post definitely does exist and load under blog.alexbowyer URL.

    Blog post 3
    Slug: science-friction-review
    Blog post exists at http://blog.alexbowyer.com/2013/01/science-friction-review/ and loads fine via that URL
    Redirect rule in effect (my attempt to fix it and stop circular redirection):

    Redirect 301 http://alexbowyer.com/2013/01/science-friction-review/ http://blog.alexbowyer.com/2013/01/science-friction-review/

    Result: When accessed via alexbowyer.com URL we get a 404.
    My interpretation: the redirect rule is failing to find a match, so the URL behaves exactly the same as if nothing exists at that URL.

    Blog post 3 illustrates another attempt to fix the circular redirects problem which also does not seem to be working. Note that the blog post definitely does exist and load under blog.alexbowyer URL.

    Hope this makes things clearer. Thanks again for continuing to engage me in this conversation. Hope you can help.

  11. Pioneer Valley Web Design
    Member
    Posted 8 months ago #

    We'll have to agree to disagree about the 404 situation at the root URL. It's irrelevant to the issue I am trying to fix anyway

    I do disagree. Something related to that site is throwing 404 errors prior to the rewrite rules.

    As for the rest, just remove the rewrite rules in root. If sub-site url's work at that point, then what I have noted is true. The issue is that all these posts at main site are returning 404. You cannot redirect a 404 with a 301 of any sort.

    Again, please consult host on this.

  12. alexbowyer
    Member
    Posted 7 months ago #

    I am indeed consulting the host on this as well, but to reply to your last assertion:

    Something related to that site is throwing 404 errors prior to the rewrite rules.

    If that were true, how do you explain the fact that blog post 1 behaves differently than blog post 2 & 3? The only difference between those posts is what's in the .htaccess file. And if I switch the rule styles around the behaviour changes accordingly. Seems to me the .htaccess file is very much being processed, and is indeed affecting the resolution of those URLs....

  13. alexbowyer
    Member
    Posted 7 months ago #

    Just updating this thread for the benefit of anyone else who might end up googling their way here with a similar problem.

    My ISP found me a partial solution. This redirects any URL that doesn't correspond to a file or directory, to the subdirectory.

    It will work until I need to use WordPress at the root level as well as at the subdirectory level. For a static site it works fine...

    These are the .htaccess lines needed:

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

Reply

You must log in to post.

About this Topic