Welcome to WebmasterWorld Guest from 54.81.220.239

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

.htaccess mod_rewrite Apache domain name IP address stumper

     
7:30 am on Jan 19, 2003 (gmt 0)

New User

10+ Year Member

joined:Dec 22, 2002
posts:6
votes: 0


This is a real puzzle and I've banged my head against the wall for a couple of hours now and just can't manage a good solution. Can anyone help?

First, let me say that I don't own the server so modifying the .htconf isn't an option -- I have to do this by .htaccess only, but I know mod_rewrite is enabled by my host.

Now, the problem:

I have a domain, username.com that is also addressable as 123.45.67.89/~username

I also have some content in 123.45.67.89/~username/foo that is [naturally] currently accessible as username.com/foo

I need to:

1) change this so that mod_rewrite redirects all requests to 123.45.67.89/~username or to 123.45.67.89/~username/ [note please the trailing slash] to 123.45.67.89/~username/foo

[that to prevent people from chopping the URL in the browser Address bar]

AND

2) redirects all traffic to username.com/foo or username.com/foo/ to username.com

[just in case somebody tries a reverse DNS lookup]

WITHOUT

3) changing -anything- else that happens anywhere on username.com or in 123.45.67.89/~username/foo

I -think- this should be possible using the REQUEST_URI environment variable, but I just can't get the syntax right.

I've tried simplifying the problem -- recognizing that the root directory /~username/ can only be used with the IP address version -- but this isn't working at all...

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/~username$ [OR]
RewriteCond %{REQUEST_URI} ^/~username/$
RewriteRule ^.*$ [123.45.67.89...] [R]

... but even that would only solve half the problem [if it worked] and not the username.com/foo access problem.

I know it's a hassle, but I can't afford a second domain name and hosting plan while I run two projects that can't be publicly associated. [Same reason I can't just put one project into a subdomain unfortunately...]

please, Please, PLEASE can someone offer an approach that works?! [RTFM? I have! 3 times!]

Thanks so much in advance for reading this and thinking about a solution.

--Bangkok9

5:22 pm on Jan 20, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 7, 2003
posts:1230
votes: 0


hi!

first of all, use canonical urls. that will prevent the trailing slash problem. stfm.

mod_rewrite redirects all requests to 23.45.67.89/~username or to 123.45.67.89/~username/ to 123.45.67.89/~username/foo (i suggest to ip/~user/foo/):

RewriteEngine on
RewriteBase /
RewriteRule ^/~username/$ [ip...]

this will do what you wrote. point 2 you made is 100% contra to this rule, so it would lead into a rewrite loop. but maybe all you wrote down is a bit chaotic and i dind't get the point.

for this kind of configuration you have tell something about your mapping: does 123.45.67.89/~username/ will access the same server namespace as username.com/? can you setup another webroot for your subdomains?

let the audience know, because some mod_rewrite freaks call webmasterworld their home.

5:59 am on Jan 21, 2003 (gmt 0)

New User

10+ Year Member

joined:Dec 22, 2002
posts:6
votes: 0


Actually, I've gotten it to work.

Put this into the .htaccess in the webroot:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/~username/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/~username$ [NC]
RewriteRule .* [123.45.67.89...] [R]

And this in the .htaccess in /foo:

RewriteEngine On
RewriteCond %{SERVER_NAME} ^.*username.com$ [NC]
RewriteRule .* [username.com...] [R]

It might not be a textbook solution, but it seems to do exactly what I had in mind.

9:52 pm on Jan 21, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 7, 2003
posts:1230
votes: 0


but quite like in a textbook. ;). fine you sorted it out.
5:00 am on Jan 22, 2003 (gmt 0)

New User

10+ Year Member

joined:Dec 22, 2002
posts:6
votes: 0


Yes, all sorted. I just had to shift my paradigm and make two .htacess instead of one.