homepage Welcome to WebmasterWorld Guest from 50.17.86.12
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
htaccess help pls
301 mod rewrite
sunzon

5+ Year Member



 
Msg#: 3003990 posted 1:36 pm on Jul 12, 2006 (gmt 0)

I am moving my sitedata from one domainname to another.
All files and directories are the same, except one directory, and that's my problem to figure out.
Situation is:
olddomain.com/dir1
olddomain.com/dir2
olddomain.com/dir2/dirx1
olddomain.com/dir2/dirx2
olddomain.com/dir3
olddomain.com/dir4
and
newdomain.com/dir1
newdomain.com/dirx1
newdomain.com/dirx2
newdomain.com/dir3
newdomain.com/dir4
in other words one directory level has been taken out (dir2) in the new domain

If all directories were the same, I could simply put in the old domain root an htaccess file like this:
----RewriteEngine On
----Redirect permanent / [newdomain.com...]
but that will not work for dir2, it will redirect to newdomain.com/dir2, newdomain.com/dir2/dirx which don't exist.

I figure that the solution would be to place another htaccess file in newdomain.com/dir2 (directory created for that purpose) that would redirect any calls for that level and subdirectories to the root newdomain.com/ but I can't figure out how to set that up. Using the same htaccess creates a loop.
Can anyone suggest a solution?
many thanks

 

RonPK

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3003990 posted 4:12 pm on Jul 12, 2006 (gmt 0)

'Redirect permanent' is not a mod_rewrite instruction; also, it will only work on individual files. In stead, try this in your root .htaccess:

RewriteEngine On
# first, redirect the special cases
RewriteRule dir2/dirx1/(.*)$ http://example.com/dirx1/$1 [R=301,L]
RewriteRule dir2/dirx2/(.*)$ http://example.com/dirx2/$1 [R=301,L]
# now, redirect the others
RewriteRule (.*) http://example.com/$1 [R=301,L]

Disclaimer: untested!

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3003990 posted 5:23 pm on Jul 12, 2006 (gmt 0)

> also, it will only work on individual files.

OT comment only: The Redirect directive works on all files which match the specified prefix. Therefore, it does work on multiple files, but is not as powerful or selective as RedirectMatch or RewriteRule, which use regular-expressions for advanced pattern-matching support.

Jim

RonPK

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3003990 posted 5:38 pm on Jul 12, 2006 (gmt 0)

Thx for the correction, Jim.

sunzon

5+ Year Member



 
Msg#: 3003990 posted 10:40 pm on Jul 12, 2006 (gmt 0)

many thanks for the response.

I get the gist, however in fact there is not just dirx1 and dirx2 -- they were examples-- there are over 200 subdirectories in dir2, so I was hoping to find a solution that doesn't require me to be dir2-subdirectory specific.

This is why I thought.... redirect all from old domain to new domain, then tackle the problem with a redirect on the new domain, find an appropriate htaccess syntax in the new domain just for dir2, to move up to the root any calls on dir2 and its subdirectories directly to the (dirx1 etc) subdirectories at the root level.

again thanks for any assistance.....

RonPK

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3003990 posted 7:50 am on Jul 13, 2006 (gmt 0)

I see.

RewriteRule dir2/(.*)/(.*)$ http://example.com/$1/$2 [R=301,L]

That should map all subdirs of dir2 on the old site to the root of the new site.

sunzon

5+ Year Member



 
Msg#: 3003990 posted 8:56 am on Jul 13, 2006 (gmt 0)

Thanks RonPK!
I found a way, but it's a workaround.

At the root of olddomain.com I put this htaccess file:
RewriteEngine On
RewriteRule dir2/(.*)$ [newdomain.com...] [R=301,L]
RewriteRule (.*) [newdomain.com...] [R=301,L]

The second line caters for all files and directories (being same structure) other than dir2 as intended.
The first line effectively caters for all subdirs (and subsubdirs and files) of dir2 moved up a level (olddomain.com/dir2/dirx1 redirects to newdomain.com/dirx1 and so on for all others: dirx2,3....dirx200) as intended.

The only single remaining problem is olddomain.com/dir2 which redirects to newdomain.com/dir2 and gives a 404.
So I created a dir2 in newdomain.com, and put in an index.html file with a http refresh redirect to newdomain.com/. That solves the problem, although unelegant.

So what should an htaccess file look like, if you want a call to newdomain.com/dir2 to redirect to newdomain.com/ (viz the root), that I could place in newdomain.com/dir2?

thanks for any help

note for clarity --better example--:
olddom/ page with links to widgets subdir and foo subdir
olddom/widgets/ (page with links to all subdirs)
olddom/widgets/widgetsblue/
olddom/widgets/widgetsgreen/
olddom/foo/ (page with links to all subdirs)
olddom/foo/foohigh
olddom/foo/foolow
at newdom I have dropped foo dir and only deal with widgets, which is why I want to move all the old widgets subdirs up a level:
newdom/ (page with links to all subdirs)
newdom/widgetsblue/
newdom/widgetsgreen/

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3003990 posted 7:07 pm on Jul 13, 2006 (gmt 0)

I would like to encourage all readers to review our forum charter [webmasterworld.com]. The purpose of this forum is to help members to learn to write their own code, not to serve as a 'free coding service.'

For basic rewrite coding 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].

Thanks,
Jim

sunzon

5+ Year Member



 
Msg#: 3003990 posted 12:00 pm on Jul 20, 2006 (gmt 0)

Humbled by having the manual thrown at me and to show I was not looking for a free coding service, here is the solution I finally figured out, for anyone reading this thread.

At the root of newdomain I put this in htaccess:

RewriteRule ^dir2/$ [newdomain.com...] [R=301,L]

which redirects any calls to content in dir2 to the domain root.
So in fact a call on dir2 in olddomain gets redirected twice, but it solves my problem (although I'm a bit worried about what search engine robots will think of this -- dir2 is the most crucial part of my site--).

Thanks RonPK for putting me on the right track

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3003990 posted 3:04 pm on Jul 20, 2006 (gmt 0)

> The only single remaining problem is olddomain.com/dir2 which redirects to newdomain.com/dir2 and gives a 404.

What happens if you precede your two rules in the .htaccess file at the root of olddomain.com with:

RewriteRule ^dir2$ http://newdomain.com/ [R=301,L]

and remove the dir2 rule in the .htaccess file at the root of newdomain?

If I take you at your word and assume that you are requesting "/dir2" from olddomain (/dir2 with no trailing slash), then that would not get redirected by your existing code, because the pattern "dir2/(.*)$" requires at least a trailing slash (with additional path-info being optional) on the request. So, the new 'test' rule would take care of those slashless dir2 requests.

Jim

sunzon

5+ Year Member



 
Msg#: 3003990 posted 11:15 pm on Jul 21, 2006 (gmt 0)

Thanks Jim, that's an inspiring idea.

I say inspiring because I believe it won't quite work as such.
The problem is some of dir2,dir3,dir4 etc have names like widgets/ (dir2/), widgets-history/ (dir3/) widgets-outofproduction/ (dir4/) and so on.
So the purposeful slash in the first rewriterule on olddomain ensured that subdirs of dir2 (the main dir) went up a level on newdomain, whilst the rest maintained structure --as intended.

Fortunately I only have 5 or so first level dirs with names that include the text "dir2", in fact all with a hyphen name as per my examples above.
So I will research on how to syntax that based on your suggestion.

thanks for your guidance,
cheers
Riny

sunzon

5+ Year Member



 
Msg#: 3003990 posted 12:11 am on Jul 22, 2006 (gmt 0)

By the way, I must say I'm a bit confused now.
Wouldn't that first new extra line make the 2nd superfluous?

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3003990 posted 12:55 am on Jul 22, 2006 (gmt 0)

I'm not sure which you're referring to as 'first' and 'second', but if the patterns are different, then no, the new rule wouldn't make the second superfluous -- although you might be able to combine them later if it works.

Jim

sunzon

5+ Year Member



 
Msg#: 3003990 posted 11:32 am on Jul 22, 2006 (gmt 0)

Works fine, indeed.
To recap, I now have this at olddomain root and no measures on newdomain:

RewriteEngine On
RewriteRule ^dir2$ [newdomain.com...] [R=301,L]
RewriteRule dir2/(.*)$ [newdomain.com...] [R=301,L]
RewriteRule (.*) [newdomain.com...] [R=301,L]

I thought regex would identify more than just "dir2", also the dir2 in "dir2-blabla" and "dir2/", so I'm a bit frustrated in not fully understanding, but happy I have a working solution. I'll have to study up on other posted scenarios to improve my insight.

Thank you for your help.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved