Forum Moderators: phranque

Message Too Old, No Replies

Redirect to relative folder

         

eventide

2:17 pm on Sep 10, 2009 (gmt 0)

10+ Year Member



Hi, all

I have a working htaccess redirect that allows only the two IP addresses I work from into a particular folder within a temp site I'm using for development of a client's site.

It looks something like this:

# Redirect all except my IP addresses to root folder for the client
# Home IP address
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4$
# Work IP address
RewriteCond %{REMOTE_ADDR} !^4\.3\.2\.1$
RewriteRule .* [dev.mysite.com...] [R=302,L]

What I'd like to do is RE-use this for other clients, so I'm wondering if I can change the RewriteRule line to take visitors to a folder RELATIVE to the folder they're trying to access. I.e. If the restricted folder is in "/myclient/privatedocs/", I want to bring them to the "/myclient/" folder, but that folder name (myclient) will be different from client to client.

Am I making sense?

jdMorgan

2:50 pm on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's really no good way to do this, since the result would be an infinite rewriting or redirection loop -- due to the fact that mod_rewrite does not have a variable-to-variable comparison function.

I would suggest one of two approaches:

1) Use client URLs like example.com/client/<clientname>, restrict accesses above /client/<clientname> to your own IP addresses, and restrict accesses *between* the subdirectories in the /clients subdirectory based on the requesting IP address -- or better yet, do both restrictions based on a username/password (to keep your clients out of each others' sites without having to know and maintain a list of all clients' home and office IP addresses -- some being dynamically-assigned and thus ever-changing).

2) If you're hosted on a server with a unique IP address (IP-based virtual host, IP-based VPS, or dedicated server) or if you can get one (typically $1.00 per month additional hosting fee in the U.S.), then you can use arbitrary ("wild-card") subdomains -- one for each client. Then map those subdomains to subdirectories below /clients, again based on the requesting IP address or based on a username/password (to keep your clients out of each others' sites).

By using an internal rewrite instead of an external redirect, this would result in the client seeing only client.example.com/ or client.dev.example.com/ (your choice) as the URL -- The client would not be aware of the subdirectory scheme at all.

The use of a /clients subdirectory path is required in order to easily detect whether the client (browser) request needs to be rewritten to one of the /clients/<clientname> subdirectories, or has already been rewritten, thus preventing the 'infinite' loop problem.

You could also do all of the above using "add-on domains" if your host allows an unlimited (or adequate) number and supports that function through the Control Panel. I prefer the wildcard-subdomain-on-unique-IP address approach though, because it allows me much more control and no restrictions on the number or form of the subdomains.

Be sure to exclude search engine robots from your clients' subdirectories, either through the login requirement or by use of robots.txt (or both).

Rewriting subdomains to subdirectories is already well-covered by previous posts in this forum. The WebmasterWorld site search feature should dig up several dozen threads on that subject.

Jim

eventide

2:54 pm on Sep 10, 2009 (gmt 0)

10+ Year Member



Hey, JD thanks for all the info and tips!

I'll take this post and work with it some more.