Forum Moderators: phranque

Message Too Old, No Replies

web folder migration- will .htaccess do the job?

Best way to forward specific folders

         

bearduk

4:26 pm on Jun 2, 2009 (gmt 0)

10+ Year Member



Hi all,

I have two folder structures on the same server

/current/website/accounts/

wherein the account folders are referenced and assigned web addresses using Apache Aliases

The second structure is

/newsite/

which has a CMS published site structure (ie apache will only be needed to point to this as DocumentRoot when the site is ready)

Unfortunately there are over 300 web accounts and migration to the new system will be slow. Therefore I need to run both file structures simultaneously.

My question is this. How can I pick out certain accounts from the Apache config and point them to the new file structure?

If I use redirect then that will work but it will also take out any sub addresses

eg www.site.com/section1 redirected will also redirect
www.site.com/section1/subaddress

If the sub address is another account that won't be updated for while then I'll be in trouble!

Has anyone any ideas as to a solution for this?

I thought maybe changing the Aliases to point to the new sections folder would be good enough but then that doesn't account for addresses with filenames

The new file structure published by the cms gives all pages a document name and one index file. Therefore if I forward any address with a filename on it eg contact.html this will be /newsite/filestructure/(index.html) on the new file structure.

Many thanks for taking a look, I'll keep you posted on progress in case someone else is following this and needs help too.

jdMorgan

6:47 pm on Jun 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do not redirect. Replace all Alias directives with AliasMatch or with mod_rewrite rules in the "internal rewrite" syntax form as needed.

This will allow you to use regular expressions patterns to match any and all URL variations as needed, and rewrite those URL variations to the correct internal filepaths. Make use of mod_rewrite's RewriteCond directive when necessary to allow for multiple-condition cases and/or to simplify the logic.

Jim

bearduk

6:36 pm on Jun 3, 2009 (gmt 0)

10+ Year Member



Thanks for the reply Jim,

unfortunately I'm by no means a dab hand at this so I'll have to get my head into the apache docs AliasMAtch RewriteCond sections. Thank you very much for pointing me in the right direction. You've helped me with a good starting point.

Chris

bearduk

10:19 pm on Jun 4, 2009 (gmt 0)

10+ Year Member



Ok, sorry if I appear a little slow here. Below I've pasted my apache rules. The original Alias is there and a new RewriteEngine rule. Now this forwards all page to the new address.

Alias /depts/cc/halls /fs/web/old/folder/home

RewriteEngine on
RewriteRule ^/depts/cc/halls/[a-zA-Z0-9_.-]+/?(.*) http://www.example.co.uk/newaddress [R]

Now this sends all addresses with a file name to the new specified address. But unfortunately if the address is entered with just a slash
ie
http://www.example.co.uk/depts/cc/halls/

then it still shows the index.htm

any ideas how to forward the index aswell?

This solution also forwards any sub folders of this address. I only want this one folder to be redirected though. Is this where you would use "RewriteCond" ? I've read the apache docs on it but it doesn't seem clear to me.

Thanks again

Chris

[edited by: jdMorgan at 11:50 pm (utc) on June 4, 2009]
[edit reason] example.com or example.co.uk [/edit]

g1smd

11:37 pm on Jun 4, 2009 (gmt 0)

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



Drop the domain name from the target of the rule and change the [R] to [L].

You need a rewrite here not a 302 redirect.

Remove the question mark from your pattern as you are making the question mark optional by leaving it in there.

jdMorgan

11:49 pm on Jun 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Where is this new RewriteRule located?

It should work in httpd.conf (or other config-level file), but it shouldn't work at all within a <Directory> container, nor within a .htaccess file...

But to answer your question, assuming you've put it in a config file:


RewriteRule ^/depts/cc/halls/([a-z0-9_.\-]+/?)?$ http://www.example.co..uk/newaddress [NC,R=301,L]

- or in .htaccess:

RewriteRule ^depts/cc/halls/([a-z0-9_.\-]+/?)?$ http://www.example.co..uk/newaddress [NC,R=301,L]

I'm not sure why you're apparently discarding the "page name" from the original request instead of back-referencing it to pass it into the new URL.

Jim

bearduk

12:12 pm on Jun 5, 2009 (gmt 0)

10+ Year Member



Thanks for the replies.

I have a main http.conf file which is referencing several virtual hosts. The vhost configs are kept in a different folder.

I'm using the rules above in the vhost for this domain and it is not within any <directory> or other tag. It's simply typed beneath the Alias.

I'm trying to forward all requests for the departmental pages because the new file naming scheme will be very different to the present one. ie if I append whatever.html to the rewritten address, then 'whatever.html' will not exist.

I'm trying to do a best effort at forwarding the old sections to the new sections and hence avoid hundred's of 404 errors. Maybe I'm going about it the wrong way..

I've tried

RewriteRule ^/depts/cc/halls/([a-z0-9_.\-]+/?)?$ http://www.example.co..uk/newaddress [NC,R=301,L]

and this now works great.

I was using [R] before as I wanted to notify search engines that the address was now changed to a new one. I see that [R=301] is the best way to let search engines know as it's a 'moved permanently' declaration; thanks for that.

Many thanks for the feedback, I'm new to this and it's much appreciated.

I think I now need to backwards engineer your rewriterule so I can get my head around how you did it !

g1smd

12:23 pm on Jun 5, 2009 (gmt 0)

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



The rule matches /depts/cc/halls/ with that part then followed by (some characters and then an optional trailing slash) or just ending without being followed by anything at all after halls/ - the placement of the two question marks is critical.

bearduk

3:25 pm on Jun 6, 2009 (gmt 0)

10+ Year Member



Thanks guys. I've realised that I need to get a real grasp on regular expressions to understand this properly. Were there any valuable tutorials or such like that you used when you first started working with regex's?
Again thanks, you're comments have been very helpful.
Chris

g1smd

4:10 pm on Jun 6, 2009 (gmt 0)

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



I searched in Google and read a whole bunch. Some were better than others. Some had errors!

jdMorgan

5:39 pm on Jun 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Our Forum Library may prove helpful as well...

Jim