Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule for change of directory name?

Please check syntax of RewriteRule line

         

Nick_Hope

4:29 pm on May 14, 2009 (gmt 0)

10+ Year Member



I have changed the name of a directory that contains hundreds of .htm and .jpg files, 2 levels deep. The directory is in my root html folder.

I want to avoid broken links and lost traffic by redirecting users to the new folder. I'm used to using Redirect permanent but as it's so many files this time I think I should be using mod_rewrite. I already have a functioning Rewrite area in my .htaccess file to prevent hotlinking, so I just need the syntax for the extra line, but I'm not confident in it. Could someone please advise if this line would do it? (note the new directory name has a hyphen in it)

RewriteRule ^/olddirectory(.*) /new-directory$1 [R=permanent,L]

Thanks!

SteveWh

8:00 pm on May 14, 2009 (gmt 0)

10+ Year Member



If you need to experiment without affecting other site visitors, you can add the first line below. Replace the digits with your own IP address. Then the rewrite will only apply if you are the one making the request.

RewriteCond %{REMOTE_ADDR} ^111\.222\.333\.444$
RewriteRule ^/olddirectory(.*) /new-directory$1 [R=permanent,L]

[edited by: SteveWh at 8:01 pm (utc) on May 14, 2009]

Nick_Hope

1:38 pm on May 15, 2009 (gmt 0)

10+ Year Member



Thanks for that Steve.

Unfortunately my redirect is not working. Typically I would like URLs such as this...

http://www.example.com/olddirectory/level1/leve2/page.htm

...to be redirected to...

http://www.example.com/new-directory/level1/leve2/page.htm

There are jpeg files below the name-change directory too. The whole structure remains the same. I've just changed the directory name. But queries are still finding the old directory.

Any help would be greatly appreciated!

[edited by: jdMorgan at 3:39 pm (utc) on May 15, 2009]
[edit reason] example.com [/edit]

Nick_Hope

2:21 pm on May 15, 2009 (gmt 0)

10+ Year Member



I got this working now, thanks. This is the successful line:

RewriteRule ^olddirectory/(.*)$ http://www.example.com/new-directory/$1 [R=301,L]

[edited by: jdMorgan at 3:40 pm (utc) on May 15, 2009]
[edit reason] example.com [/edit]

jdMorgan

3:12 pm on May 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a comment:

This is a case where it is not clear that the URL needs to be changed at all. It would be possible to simply rewrite requests for the old URL to the new filepath, without disturbing the URLs or their current search rankings at all.

With all of the URLs changing all at once, it may take several weeks to nine months to recover the previous page rankings in search.

As I said, it's not clear. It may be in fact be what is really desired or needed in this specific case. But in many if not most cases a change in the server's internal file structure does not require a change in the public URLs. An internal rewrite would suffice:


RewriteRule ^olddirectory/(.*)$ /new-directory/$1 [L]

Or, if it is desired to leave some Web-accessible content in "/olddirectory" then either

RewriteCond %{DOCUMENT_ROOT}/newdirectory/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/newdirectory/$1 -d
RewriteRule ^olddirectory/(.*)$ /new-directory/$1 [L]

-or-

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^olddirectory/(.*)$ /new-directory/$1 [L]

would respectively check to see if the requested content exists in newdirectory or does not exist in olddirectory before doing the rewrite.

Hypertext Style: Cool URIs don't change. [w3.org]

Jim

g1smd

11:03 am on May 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



How are you getting on with this?

Nick_Hope

12:11 pm on May 21, 2009 (gmt 0)

10+ Year Member



The line I quoted in my previous post is working fine thanks.

Thanks to Jim for the comments about URIs possibly not needing to change. I considered hard, based on what I know about SEO etc., and in this case I decided to rename the directory for simpler management locally on my machine, and for a better logical structure as seen by users and the search engines. I'm also updating titles and descriptions as part of an overhaul.

The few pages from that directory that do well in a Google search, still point to the old directory name in the search results. I'll wait and see what happens to the SERPs when Google re-indexes. I'm hoping they won't drop, but if they do, hopefully other pages in the directory will at least make up for it with higher positions.

jdMorgan

1:28 pm on May 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, but my main point was that a change of server directory structure does not require a change in URLs published on the Web, and that URL changes should be avoided.

Jim