Forum Moderators: phranque

Message Too Old, No Replies

Redirecting old folder to new folder

         

ForumKid1

12:06 pm on Aug 24, 2007 (gmt 0)

10+ Year Member



This is what I'm trying to accomplish, but it's not working.

The site was laid out as such:
http://www.example.com/oldfolder/

What I want is that any url going to oldfolder, to go to newfolder as such:
http://www.example.com/newfolder/

This is my apache virtual host code, but it doesn't seem to be doing anything at all:

RewriteRule ^/oldfolder/$ http://www.example.com/newfolder/$ [R=301,L]

Is it because I do not have a RewriteCond?

jdMorgan

2:02 pm on Aug 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That will only redirect a request for the /oldfolder folder itself, and not for any files inside that folder.

Assuming that this code is in httpd.conf, try:


RewriteRule ^/oldfolder[b]/(.*)$[/b] http://www.example.com/newfolder[b]/$1[/b] [R=301,L]

The $1 in the substitution URL on the right back-references (takes the value of) the requested URL-path substring matching the parenthesized subpattern on the left. In this way, all files in /oldfolder are redirected to the same-named files in /newfolder.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

ForumKid1

4:03 pm on Aug 24, 2007 (gmt 0)

10+ Year Member



Ahh, awesome. I never did understand those 2 variables. Works great.

My last question would be, why doesn't it actually rewrite the url? I mean, the only reason I would care is for SEO reasons..Not sure if it even matters though. The url still shows oldfolder in the address bar, yet it definitely redirected to newfolder....

g1smd

4:16 pm on Aug 24, 2007 (gmt 0)

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



If it redirected then the new URL should show. The [R=301] forces the browser to request the new URL in a new HTTP transaction.

On the other hand, a rewrite pulls content from the new location while showing the old URL. A rewite is almost the same code but without the [R=301] bit.

jdMorgan

4:26 pm on Aug 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code above is a redirect, and will change the browser address bar. But you must always completely flush your browser cache before testing any change to your configuration code.

In this case, if /oldfolder/foo.html is cached in your browser and you request it again, the browser will render that page from its cache, and no request will be sent to your server. Therefore, the rule can have no effect.

Jim

ForumKid1

1:31 am on Aug 25, 2007 (gmt 0)

10+ Year Member



Cache it was. All works awesome now. Thanks!