cancel
Showing results for 
Search instead for 
Did you mean: 

301 Redirect in htaccess for existing Wordpress site.

jtonline
Grafter
Posts: 83
Thanks: 9
Registered: ‎21-03-2008

301 Redirect in htaccess for existing Wordpress site.

I'm having trouble getting my head around the general expressions I need to add to my htaccess file for use with Wordpress now that I'm using it with jtonline.info newly hosted by Plusnet.
I have an existing Wordpress site in a directory called 'blog' off the root of ccgi.  Since I pointed my domain to the root of the ccgi server I have moved the Wordpress index.php and .htaccess files to the root, amended the Wordpress site & url fields via the dashboard settings and performed an update of my Permalinks as per the Wordpress instructions about having Wordpress in its own directory.
Wordpress is working correctly using the new domain name, however, I would like to have all instances of the old address that are around permanently redirected to the new url structure.  So, for example -
  http://ccgi.jtonline.plus.com/blog/site-map/
I should like redirected to -
  http://www.jtonline.info/site-map/
Note that the 'ccgi.username.plus.com' bit is replaced with 'www.jtonline.info/' and the '/blog' bit has been removed, then whatever happens to be in the url after '/blog' is added back on.
I should be grateful if someone could just post the code I need to add to the .htaccess file so that I can get this sorted, and then I'll try and learn for myself how it's done in due course.
My current .htaccess is as follows:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
</IfModule>

Many thanks.
4 REPLIES 4
spraxyt
Resting Legend
Posts: 10,063
Thanks: 674
Fixes: 75
Registered: ‎06-04-2007

Re: 301 Redirect in htaccess for existing Wordpress site.

I think what you need (at the top of that .htaccess file) is

RewriteEngine On
RewriteCond %{HTTP_HOST} ^ccgi\.jtonline\.plus\.com$ [NC]
RewriteRule ^blog/(.*) http://www.jtonline.info/$1 ; [R,L]
David
jtonline
Grafter
Posts: 83
Thanks: 9
Registered: ‎21-03-2008

Re: 301 Redirect in htaccess for existing Wordpress site.

Thanks so much spraxyt, that did the trick nicely.  It looks like I was being overly complex with the expressions I was trying to use and wasn't getting anywhere, hence the cry for help.
As I understand it, the first line you've used sets a condition to look for http:// and then anything that matches the old url in upper or lowercase.
The second line then starts at the blog/ part of the url and stores all that follows it in $1 and then finally redirects to the new url with the contents of $1 appended.  The R means it's a redirect and the L says it's the last rule for the condition.
Since I've done a bit of testing and it all appears to be working, I've changed the [R,L] to [R=301,L] so that search engines know that it's a permanent redirect.
The redirect broke the display of images in my posts because the media upload url gets hardcoded into the post content, but I fixed that by using phpmyadmin to do a search and replace on the database.
Thanks again for your help.
Jules.
spraxyt
Resting Legend
Posts: 10,063
Thanks: 674
Fixes: 75
Registered: ‎06-04-2007

Re: 301 Redirect in htaccess for existing Wordpress site.

Pleased to hear that worked, and making it R=301 is correct for a permanent redirect. Smiley
The way rewrite rules work is only partly top to bottom. So what happens here is that in server-context the URI is stripped of the scheme (HTTP) and hostname (www.jtonline.info OR ccgi.jtonline.plus.com) parts and mapped to the (same) physical file system path. In per-directory context (.htaccess) the physical path part up to and including the / before the current directory is then stripped and matched against RewriteRule patterns starting from the top of the .htaccess file.
Hence http://www.jtonline.info/blog/anything OR http://ccgi.jtonline.plus.com/blog/anything will match the ^blog/(.*) pattern so the rule is processed (with $1 set to the string anything). It goes back to RewriteConditions associated with that RewriteRule now, working through these top to bottom (if more than one).
Our first RewriteCondition tests if the hostname is ccgi.jtonline.plus.com (the backslashes escape the full stops to make them real ones and not meta characters that match any character). As you said the NC flag is given so the comparison is not case sensitive. The www. form fails that so further processing of the rule stops and the next rule is examined. The ccgi. form matches, and since there are no more conditions substitution on the RewriteRule takes place as you described. The flag R=301 causes the substitution to be processed as a permanent redirect, and L makes this the last rule processed. After the redirect processing starts again, but this time with the www. form of hostname and without /blog; so this rule is skipped.
I think the reason images weren't located is because they aren't where this rule would redirect the search. Changing the database patched that problem from posts, but a direct request (eg from a search engine link) will still fail. I think another rule is needed to cover this - to be covered later …
David
David
jtonline
Grafter
Posts: 83
Thanks: 9
Registered: ‎21-03-2008

Re: 301 Redirect in htaccess for existing Wordpress site.

Quote from: spraxyt
... I think the reason images weren't located is because they aren't where this rule would redirect the search. Changing the database patched that problem from posts, but a direct request (eg from a search engine link) will still fail. I think another rule is needed to cover this - to be covered later …...

The images uploaded for display in the posts require the /blog directory to be in the URI and the full URI is hardcoded into the post (not relative).  It was a quick process to use phpmyadmin to search the post content table in the database and replace all instances of the ccgi bit with the new domain name.
None of the images inside the posts should be viewed by a direct request, only the post URI, so patching the database is OK.