Forum Moderators: phranque
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
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!
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
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.....
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/
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
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
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]
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
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
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.