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