Welcome to WebmasterWorld Guest from 3.81.29.226

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Wildcard 301 after renaming a wordpress installation directory

I need help :)

     
12:26 pm on Apr 16, 2015 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 3, 2002
posts:2586
votes: 3


I have very limited htaccess experience, but I knew just enough to do what I needed to do on my old html site. Now, I am using Wordpress and for various reasons, I had to change the installation directory. Generally, this renaming has very little impact on my site's URL structure. The main, noticeable difference will be the stylesheet, javascript, and plugin source links when viewing the source code. I don't think those will have any impact on my SEO.

What I need help with is generating a 301, I assume a wildcard 301, to handle the previous media urls. How would you guys handle this?

In cpanel, my old path was:
username/public_html/blog *changed to*
username/public_html/newdirectory

So, old media/image urls of the form:

http://www.example.com/blog/wp-content/uploads/image.png
*changed to*
http://www.example.com/newdirectory/wp-content/uploads/image.png

There may be other files that I can't think of right now that may need to be marked with a 301 in htaccess. I guess the question in my mind is how far should I go with this? Do I need to accommodate all of the plugin stylesheet urls, javascript, etc? Should I just worry about the media files?

I would love some advice on this... first, assuming just the media files, what 301 would you guys recommend? Second, how deep should I go (my question above, with respect to stylesheets/javascript/plugins, etc.)?

Thank you in advance
4:01 pm on Apr 16, 2015 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:4558
votes: 362


Your WP is actually installed in a different directory or the directory it was in was renamed? The General Settings in your WP Dashboard need to be updated and the content would automatically reflect the change for media, css, images, plugins.

Or maybe I didn't understand the question completely.
4:31 pm on Apr 16, 2015 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 3, 2002
posts:2586
votes: 3


I have WP installed in a subdirectory but I have it set up to run as if it were installed in the root. Nevertheless, nothing about the change was reflected in htaccess. There are no records of 301 in htaccess and a server header check of the old media urls shows 404. I need to manually configure 301s and that's what I'm asking about, if anyone can offer a suggestion for how to do that, I would be very appreciative. I don't like the idea of manually listing each media file with a 301 to the new one. That sounds very tedious.
6:14 pm on Apr 16, 2015 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 3, 2002
posts:2586
votes: 3


ok... to give it my best shot at creating the 301, to prove that I'm not seeking free "do the work for me so I can watch Springer all day" help (lol). I wanted to show what I've come up with. From the testing I've done, I am getting 301s on the old media urls that I'm concerned about. Do you guys see anything that could go wrong with this 301?

RedirectMatch 301 /blog/wp-content(.*) /newdirectory/wp-content$1

To recap:
I renamed my wordpress installation directory from /blog to /newdirectory
The reason I added the additional subdirectory /wp-content in my 301 was because I also have a wordpress category called "blog". Therefore, if I attempt to only redirect /blog to /newdirectory without appending /wp-content, then the blog category gets redirected and everything fails. That's neither here nor there, I just wanted to explain why my redirect is in the form of /blog/wp-content.

As I explained in the original post above, the fact that I have a category named "blog" and wordpress was installed in a directory named "blog" is the reason I had to change the name of the directory. The naming conflict caused a redirect loop since I access WP from the root (I don't have the energy to explain it any better, just trust me on that :) )

Soooo, long story short, does that rule look safe (i.e., the dollar sign at the end, etc.)?

I know there are some good htaccess experts in this forum, so if you guys see anything unsafe about it, please let me know.

[edited by: crobb305 at 6:19 pm (utc) on Apr 16, 2015]

6:18 pm on Apr 16, 2015 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:4558
votes: 362


You don't want to use RedirectMatch for this. Quoting lucy24:
Redirect (or RedirectMatch). mod_alias (Redirect by that name) and mod_rewrite (RewriteRule, the core of any WP site) do not play nice together. So make sure you get rid of every last occurrence of Redirect-by-that-name in all htaccess files everywhere.


You could add in a rule to change the directory from /blog/ to /newdirectory/ and capture the string after the directory:
RewriteRule ^/blog/(.*)$ /newdirectory/$1 [R=301,L]

this should be in the htaccess file before the WP standard lines. It will need to be in both htaccess files.

[edited by: not2easy at 6:23 pm (utc) on Apr 16, 2015]

6:22 pm on Apr 16, 2015 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 3, 2002
posts:2586
votes: 3


You could add in a rule to change the directory from /blog/ to /newdirectory/ and capture the string after the directory:
RewriteRule ^/blog/(.*)$ /newdirectory/$1 [R=301,L]
this should be in the htaccess file before the WP standard lines.


not2easy, would that prevent my wordpress category "blog" from being redirected? I don't want that to happen. It's a big mess -- the blog CATEGORY and the blog DIRECTORY. I did try a /blog/ wildcard earlier, and it didn't work... any posts in the /blog category were being sent into a redirect loop (for example, /blog/a-post-about-something returned an infinite loop error in the browser).
6:29 pm on Apr 16, 2015 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:4558
votes: 362


Sorry, edited my previous reply because I can't see the post while I'm "codifying". It may be more clear now. The RedirectMatch can cause a variety of unwelcome surprises when used with Rewrite Rules which are a standard part of the WP htacess.

If you have a WP category called /blog/ then yes, it could cause that to be rewritten back to the home page. When you do a wildcard rewrite rule, it applies to all instances of /blog/ in any URL.
6:32 pm on Apr 16, 2015 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:4558
votes: 362


IF you have changed the Settings file to show the /newdirectory/ then only inbound links would need to be rewritten. WP would no continue looking for URLs (or media) in /blog/ other than the /blog/ category.

That changes what you want your rule to do.
6:38 pm on Apr 16, 2015 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 3, 2002
posts:2586
votes: 3


Using your rule, I've tested some of the old image urls that contain mysite.com/blog/wp-content/uploads... and the rule isn't working. /blog/ is not redirecting to /newdirectory/ I'm being sent to a 404.

My rule was working, but apparently there's something wrong with doing the RedirectMatch. Can my rule be used as a Redirect? It's critical that I redirect /blog/wp-content/ to /newdirectory/wp-content/ In order to achieve 301 on my media files.
6:45 pm on Apr 16, 2015 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15928
votes: 884


RedirectMatch 301 /blog/wp-content(.*) /newdirectory/wp-content$1

Get rid of this. By the usual yawn-provoking coincidence it is no more than 24 hours since someone in an unrelated thread said he had been doing this successfully for years in a WP installation. But just because the Apache fairies were in a preternaturally good mood on his site is no reason to assume they will do the same on yours.

Use mod_rewrite (RewriteRule) for all redirects. Any new rules you add have to go before the WP envelope in your htaccess. (If the line "RewriteEngine on" is currently inside the WP envelope, repeat it before all new rules.)

It's theoretically possible to find WP settings to handle all redirects, 404s and maybe even 410s for you. But this is putting your server to a lot of extra work. (WP is many things, but nobody will ever call it an efficient use of server resources.)

So, when you have a rule in the form
RedirectMatch 301 /blog/wp-content(.*) /newdirectory/wp-content$1 
you need to change it to say
RewriteRule ^blog/wp-content/(.*) http://www.example.com//newdirectory/wp-content/$1 [R=301,L]
Don't overlook the flags! Of course the rule could be worded as
RewriteRule ^blog/(wp-content/.*) http://www.example.com//newdirectory/$1 [R=301,L]
but why bother capturing something that's always the same.

Use the full protocol-plus-domain in your target. Otherwise you may end up with a chained redirect. Can we assume that the URLs you're redirecting never end in /index.html or /index.php? If they did, you'd need one more rule to intercept those.

If you have a whole bunch of existing rules using Redirect or RedirectMatch, there's even a global replace you can run in your text editor to change them all to valid RewriteRules. But it looks as if you're creating new rules.


On the philosophical question of how many rules you need: It's really your choice whether you want to redirect non-page files that have moved to a new location. Generally I don't bother-- except that if you don't redirect them, search engine spiders (Googlebot and so on) will continue asking for them at the old URL, and then WP itself has to go look for them. So in this case you're better off explicitly redirecting everything you can think of. Requests for old URLs will never come in from humans; those will get the correct link from the current page. But why should the server have to waste time looking up old URLs when you already know where the file has moved to.
6:53 pm on Apr 16, 2015 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 3, 2002
posts:2586
votes: 3


Get rid of this. By the usual yawn-provoking coincidence it is no more than 24 hours since someone in an unrelated thread said he had been doing this successfully for years in a WP installation. But just because the Apache fairies were in a preternaturally good mood on his site is no reason to assume they will do the same on yours.

Lucy24 --
Thank you for the response, but I have absolutely no idea what you're talking about. I haven't seen such a thread, nor have I been visited by the Apache fairies of which you speak.

I was seeking a simple, polite recommendation to accommodate MEDIA files (wp-content/uploads, because people have been linking to my images and I would like to preserve what little link juice I can via appropriate 301). I had no idea this would become a smack down. Thank you, but I will find a solution on my own. Geez, this used to be a friendly forum. May Tedster rest in peace.
8:05 pm on Apr 16, 2015 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:4558
votes: 362


The thread is here: [webmasterworld.com...] your question sent me there because it reminded me that we just went through this for a similar situation. It does pay to look at other threads in the forum, often you can find what you need and sometimes pick up a little extra understanding of the potholes in the road.

You can add the suggested rewrite rule to the root directory but you may need to modify that to accommodate only THE_REQUEST to handle inbound links.

We are all busy web people like you are. I am sorry if you find our assistance offensive. I wish you luck and goodwill anyway.