Forum Moderators: phranque

Message Too Old, No Replies

Rewrite a domain to a folder in Apache

need to use .htaccess to rewrite a domain to a folder

         

jimhap

7:01 pm on Mar 28, 2009 (gmt 0)

10+ Year Member



I need help with my .htaccess.

I have a domain, say xyz.us.to.
I would like to rewrite (not redirect) xyz.us.to to /xyz and all its files, dirs, subdirectories, etc. to /xyz/(files, etc.).

(/ is document root in .htaccess)

This is my current .htaccess:
RewriteEngine on

RewriteCond %{HTTP_HOST} ^xyz\.example\.com [NC]
#RewriteRule ^/*$ /xyz/$1 [L]
RewriteCond %{REQUEST_URI} ^/123
RewriteRule ^(.*)$ /xyz/$1 [L]

(therefore, [xyz.example.com...] goes to /xyz/123)

However, I can only hard code it (folder 123), which would take too long to write. Is there a better way, preferably using regular expressions?

Thanks in advance! :)
jimhap

[edited by: jdMorgan at 2:49 am (utc) on Mar. 31, 2009]
[edit reason] example.com [/edit]

jdMorgan

12:00 am on Mar 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remove that RewriteCond if you want to rewrite all requests.

However, you will need to explicitly stop recursion by adding a different RewriteCond:


RewriteCond $1 !^xyz/

This prevents the rule from rewriting requests for /xyz/abc to /xyz/xyz/abc to /xyz/xyz/xyz/abc... ad infinitum.

Jim

g1smd

12:53 am on Mar 29, 2009 (gmt 0)

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



Your first rule looks odd. I assume there's a typo in ^/*$ as that means match zero to many slashes. Additionally, there's nowhere bracketed in the pattern to populate the $1 backreference for the target.

jdMorgan

3:03 am on Mar 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That first rule is commented-out, though. I assumed it was simply a 'test step' left in for now, but disabled.

Jim

jimhap

3:51 am on Mar 30, 2009 (gmt 0)

10+ Year Member



@jdMorgan: Yeah, it is. Hmm, so that's why I've been getting 500 Internal Server errors. In the error log, it states something about a loop...

Now it's almost there... but there's one other thing.

New .htaccess:
RewriteEngine on

RewriteCond %{HTTP_HOST} xyz\.example\.com [NC]
RewriteCond $1 !^xyz/
RewriteRule ^(.*)$ /xyz/$1 [L]

So... if I go to xyz.example.com/123 it throws me to xyz.example.com/xyz/123. It does point to the right directory, though.

Is there any way to fix that?

Thanks!
jimhap

[edited by: jdMorgan at 2:50 am (utc) on Mar. 31, 2009]
[edit reason] example.com [/edit]

jdMorgan

5:20 pm on Mar 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So... if I go to xyz.example.com/123 it throws me to xyz.example.com/xyz/123. It does point to the right directory, though.

Is there any way to fix that?

This is unclear. Fix what, specifically? What do you mean by "throws me to"?

Jim

[edited by: jdMorgan at 2:51 am (utc) on Mar. 31, 2009]

g1smd

6:45 pm on Mar 30, 2009 (gmt 0)

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



Are you saying you are redirected to another URL?

The code above should be for a rewrite.

jimhap

8:42 pm on Mar 30, 2009 (gmt 0)

10+ Year Member



It redirects me to xyz.example.com/xyz/123 instead of doing a rewrite.

That's what I mean by "throws".

[edited by: jdMorgan at 2:51 am (utc) on Mar. 31, 2009]
[edit reason] example.com [/edit]

jdMorgan

9:06 pm on Mar 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case, either you've got another directive (a redirect) in this .htaccess file that's doing the redirect --possibly because the rules are not in the correct order (always put external redirects before internal rewrites, and order the rules of each type from most-specific pattern to least-specific)-- or you've got a redirect in an .htaccess file in a subdirectory in the physical filepath to /123, or you're calling a script that is doing a redirect.

There's nothing in the rule posted above that would invoke a redirect.

Jim

jimhap

10:07 pm on Apr 2, 2009 (gmt 0)

10+ Year Member



Sorry for long reply. It is fixed.
On one server, it doesn't work. (My test server)

On others, works perfectly. Thanks! :)