Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Best way(s) to redirect urls from root site to subdirectory in WordPress
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Best way(s) to redirect urls from root site to subdirectory in WordPress

jetchiragjetchirag Member
edited May 2018 in General

Hey,

I've moved one site from / to /blog whereas there's another app in /. I'm trying to preserve old links to site therefore finding ways to redirect all urls from root to blog only for old site.

Only way I seem to find without having to touch other files for root application is to add all links to htaccess for redirections which would get messy and not-so-ideal.

It's a cpanel server account, although there are not much options, any recommendation is appreciated.

Thanks

Comments

  • YmpkerYmpker Member

    Isn't there a Wordpress plugin for find&replace sorta redirecting/changing url redirects (usually used for migration when the url changes)? Might be faster^^

    Thanked by 1jetchirag
  • Ympker said: Isn't there a Wordpress plugin

    He said there's another app in / so I don't think wordpress plugin would be of any help.

    @jetchirag how many urls do you need to redirect?

    Thanked by 1Ympker
  • It's around 97 posts

  • AITHostingAITHosting Member
    edited May 2018

    Set a wild card redirect in .htaccess file in root folder for the domain.

    Here it is:

    RewriteRule ^(.*)$ http://yourdomain.com/blog/$1 [R=301,L]

    Don't forget to change the domain to yours. :)

    Just performed some testing, works for me.

    I had a post at http://domain.com/do-you-like-reading/

    I've moved the site to /blog in File Manager and change the URLs in the database.

    Then I put the aforementioned rule in .htaccess in domain.com folder.

    And it works just fine (I edited wget output, erased all personal info).

    wget http://domain.com/do-you-like-reading/

    Resolving domain.com (domain.com)...

    HTTP request sent, awaiting response... 301 Moved Permanently

    Location: http://domain.com/blog/do-you-like-reading/ [following]

    Tried with other posts, all good.

    UPD. If you don't need to redirect domain.com itself, just sublinks, let me know, I'll do some testing with another rule.

    Thanked by 1jetchirag
  • @Xenia wouldn't that redirect all links?

  • Yes, wild card redirect will redirect all links, including domain.com and sublinks.
    So, you need to redirect those 97 posts only? Not all URLs?
    Tell me more about your main goal, I'll see if I can help you.

    Thanked by 1jetchirag
  • You can do it via .htaccess, put all the URLs to redirect like this;

    Redirect 301 /my-post-1 /blog/my-post-1
    Redirect 301 /my-post-2 /blog/my-post-2
    

    It's going to be messy and if you don't like that you can try something like this;

    $oldUrls = [
        '/my-post-1',
        '/my-post-2',
        '/my-post-3',
        '/my-post-4',
        '/my-post-5'
    ];
    $requestUri = parse_url($_SERVER['REQUEST_URI']);
    $currentPath = $requestUri['path'];
    if ( in_array($currentPath, $oldUrls) ) {
        header("Location: https://domain.tld/blog".$currentPath, true, 301);
        exit;
    }
    

    It's not tested and you will have to put this on top of index.php or something before you app is loaded.

    Thanked by 1jetchirag
  • @Xenia said:
    Yes, wild card redirect will redirect all links, including domain.com and sublinks.
    So, you need to redirect those 97 posts only? Not all URLs?
    Tell me more about your main goal, I'll see if I can help you.

    Yes, those urls only.

    @scorcher9 been considering that, thanks for code.

  • @jetchirag said:
    Yes, those urls only.

    Okay, I misunderstood you (you said you have an app in /, I did not think you need a website with working sublinks as well there.
    Sorry about that.
    Agree with scorcher9.

    Thanked by 1jetchirag
  • FalzoFalzo Member

    @jetchirag depending on your permalink-structure for your posts you might still be able to use wildcards in redirects. there also some generators to be found online that might help to create a feasible regexp rule...

  • @Falzo said:
    @jetchirag depending on your permalink-structure for your posts you might still be able to use wildcards in redirects. there also some generators to be found online that might help to create a feasible regexp rule...

    It's simple /xyz. I don't think it can be distinguished from normal links.

  • FalzoFalzo Member

    @jetchirag said:

    @Falzo said:
    @jetchirag depending on your permalink-structure for your posts you might still be able to use wildcards in redirects. there also some generators to be found online that might help to create a feasible regexp rule...

    It's simple /xyz. I don't think it can be distinguished from normal links.

    so all different? if you have access to the vhost config you could have a look into rewritemap to set up a mapping file for your url-list...

  • @Falzo, I don't have access as this is not my personal project. Helping some else moving his stuff!

    Anyways, It got bit messy and had to decide b/w php and htaccess but got it done (I guess?) with htaccess.

  • FalzoFalzo Member

    @jetchirag said:
    @Falzo, I don't have access as this is not my personal project. Helping some else moving his stuff!

    Anyways, It got bit messy and had to decide b/w php and htaccess but got it done (I guess?) with htaccess.

    grats, I'd always go for .htaccess instead of involving php as it's supposed to be the faster.

    Thanked by 1jetchirag
  • jvnadrjvnadr Member
    edited May 2018

    The easiest solution that is working using htaccess (tested and used by me in several sites):

    RewriteEngine On
    RewriteRule (.HEREGOESTHEOLDLINK) http://HEREGOESTHENEWLINK$1 [R=301,L]
    RewriteRule (.HEREGOESTHEOLDLINK2) http://HEREGOESTHENEWLINK2$1 [R=301,L]
    You can even redirect to new server with subdomain using this way

  • jetchiragjetchirag Member
    edited May 2018

    @jvnadr said:
    The easiest solution that is working using htaccess (tested and used by me in several sites):

    RewriteEngine On
    RewriteRule (.HEREGOESTHEOLDLINK) http://HEREGOESTHENEWLINK$1 [R=301,L]
    RewriteRule (.HEREGOESTHEOLDLINK2) http://HEREGOESTHENEWLINK2$1 [R=301,L]
    You can even redirect to new server with subdomain using this way

    I did Redirect 301 /xyz /blog/xyz, was neater.

Sign In or Register to comment.