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.
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
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
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^^
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?
It's around 97 posts
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.
@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.
You can do it via .htaccess, put all the URLs to redirect like this;
It's going to be messy and if you don't like that you can try something like this;
It's not tested and you will have to put this on top of
index.php
or something before you app is loaded.Yes, those urls only.
@scorcher9 been considering that, thanks for code.
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.
@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.
grats, I'd always go for .htaccess instead of involving php as it's supposed to be the faster.
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.