Forum Moderators: phranque

Message Too Old, No Replies

htaccess: "string replacement"-type 301 redirect?

from subdir to root, but with everything else preserved

         

jewelstores

5:55 pm on Oct 4, 2009 (gmt 0)

10+ Year Member



Hi all,

Say we have a site located at http://www.example.com/subfolder/.
our SEO guys are telling us to move everything from /subfolder/ to the root, and create a 301 redirect rule, so that any request containing /subfolder/ gets redirected to a similar URL, just *without* /subfolder/.

All parameters, etc. must be preserved. (Important!)

Examples:

  • Old URL: http://www.example.com/subfolder/a.html
    301 TO: http://www.example.com/a.html

  • Old URL: http://www.example.com/subfolder/index.php?param1=a&param2=b
    301 TO: http://www.example.com/index.php?param1=a&param2=b

So, the general idea is to replace all instances of "example.com/subfolder/" with "example.com/", without touching anything else, and 301-redirect to the resulting string.

How can this be done?

Oh, and we already have a "non-www to www" redirect rule in our htaccess, so where should the new rule be placed? (before or after?)

Thanks in advance!

acimag

3:47 am on Oct 5, 2009 (gmt 0)

10+ Year Member



check that out
[webmasterworld.com...]

jdMorgan

2:53 pm on Oct 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a fairly trivial application of mod_rewrite, and should be easy to implement making reference to the Apache mod_rewrite documentation and the URL Rewriting Guide linked from that document.

When trying to establish rule order, a good rule of thumb is to put all external redirects first, ordered from most-specific (fewest URLs affected) to least specific ("catch-alls" such as domain canonicalization), followed by all internal rewrites, again ordered from most- to least-specific.

Please review the documentation cited in our Apache Forum Charter, and then post you best-effort code as a basis for discussion.

Thanks,
Jim

jewelstores

2:57 pm on Oct 5, 2009 (gmt 0)

10+ Year Member



Thanks, but I also want to make absolutely sure that all parameters etc. will be preserved

I'm very much a newbie with htaccess but I know there's a catch or two about parameters... and the case in your thread sounds a bit different. Glad to see you've received a solution though.

if anyone could help with my specific situation above it would be highly appreciated... thanks again!

jewelstores

3:00 pm on Oct 5, 2009 (gmt 0)

10+ Year Member



Oops - my above message was directed to acimag... sorry for the mixup.

Thanks Jim, I'll give that a go.

jdMorgan

3:15 pm on Oct 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just by way of "reassurance," you should know that the default behaviour of Apache's redirection directives is to pass-through query strings completely unchanged. I.e. if you make no effort to manipulate the query string, then whatever query was attached to the 'old' URL ends up attached to the 'new' URL by default.

Jim

jewelstores

5:13 pm on Oct 5, 2009 (gmt 0)

10+ Year Member



Okay - after some reading and testing, this code seems to be working for me (in the root .htaccess):

# Test-subfolder to root 
RewriteRule ^subfolder/(.*) /$1 [R=301]

I don't suppose there anything else to watch for? any special caveats? (Yes, I know this is a simple matter... but I don't really trust myself yet and it's vital that our search rankings won't suffer).

Thanks for the guidance!

jdMorgan

6:07 pm on Oct 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) Always specify your canonical hostname in external redirects, in order to avoid problems if your ServerName is *not* the canonical name, and you host sets UseCanonicalName to 'on'.
2) Always use an [L] flag on your rules, unless you know why you don't want to.
3) The more meaningful and descriptive your comments, the less you will kick yourself later. :)

# Externally redirect all requests for test-subfolder resources to corresponding resources in root
RewriteRule ^subfolder/(.*)$ http://www.example.com/$1 [R=301,L]

Jim

jewelstores

10:10 pm on Oct 5, 2009 (gmt 0)

10+ Year Member



Yes, I see your points. :)
I've just moved the site and changed the rule according to your suggestion - looks like it's all working without a hitch. Thanks again!