Forum Moderators: phranque

Message Too Old, No Replies

Rewrite Subdomain to Directory and Hide Directory

Trying to rewrite a subdomain to a directory and hide the directory in URL

         

AxelAG

5:22 pm on Jul 18, 2007 (gmt 0)

10+ Year Member



I am trying to rewrite subdomains to subdirectories dynamically as each user signs up and it is working. The issue that I am having has to do with the directory showing up.

For example, http://user1.example.com should take the browser to http://user1.example.com/profiles/user1/ and this works.

However, I need the URL to stay as http://user1.example.com/.

Below is the .htaccess file in the document root.

Please help!

------------------------------------------------
DirectoryIndex index.php index.html index.htm

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ([^.]+)\.example.com [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule ^$ /profiles/%1
------------------------------------------------

[edited by: jdMorgan at 6:11 pm (utc) on July 18, 2007]
[edit reason] Example.com [/edit]

g1smd

5:30 pm on Jul 18, 2007 (gmt 0)

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



>> should take the browser to user1.example.com/profiles/user1/ and this works. <<

... then that is a redirect if the browser actually "goes" there.

>> However, I need the URL to stay as user1.example.com/ <<

... then that is a rewrite if the actual filepath remains hidden.

.

Which do you want? A redirect or a rewrite?

You need to be clear on the differences between them.

You also need to be clear that the requested URL and the server filepath/filename are two completely different things.

[edited by: jdMorgan at 6:12 pm (utc) on July 18, 2007]
[edit reason] Example.com [/edit]

AxelAG

5:44 pm on Jul 18, 2007 (gmt 0)

10+ Year Member



What I need is a rewrite, but what it seems that I am getting right now is a redirect.

g1smd

5:48 pm on Jul 18, 2007 (gmt 0)

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



A RewriteRule with an [R] is a redirect (and you can specify whether it is a 301 or a 302 redirect).

Without the [R] it should only be a rewrite.

AxelAG

5:52 pm on Jul 18, 2007 (gmt 0)

10+ Year Member



Well I dont have an [R] and I'm getting a redirect.

[If I request] http://rocky.example.com/ the browser will take [me] to http://example.com/blocks/rocky

Please advise?

Abhi

[edited by: jdMorgan at 6:13 pm (utc) on July 18, 2007]
[edit reason] No URLs, please. See Terms of Service. [/edit]

g1smd

6:02 pm on Jul 18, 2007 (gmt 0)

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



Are you sure you haven't got another rule after this one that is doing that?

What happens if you add an [L] to your existing rewrite?

AxelAG

1:01 am on Jul 21, 2007 (gmt 0)

10+ Year Member



There is nothing after that rule, in fact it is the end of the file.

Please help!?!

jdMorgan

4:15 am on Jul 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



After any rewrite or redirect is done, all mod_rewrite, mod_alias, mod_dir, and URL-based mod_access directives in the .htaccess files will be processed again, in case there are further rewrites, redirects, or access restrictions which apply to the new URL. So the fact that "this is the last rule in the file" is technically meaningless. You must treat .htaccess as fully-recursive.

What matters is this: Are there any mod_rewrite or mod_alias directives in this .htaccess file (or in .htaccess files in the subdirectories to which this rule rewrites) which would match the rewritten URL and perform an external redirect?

Jim

AxelAG

4:40 am on Jul 21, 2007 (gmt 0)

10+ Year Member



This is the only .htaccess file in the directory structure.

Here is the full file...
--------------------------

DirectoryIndex index.php index.html index.htm

CheckSpelling On

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule ^$ /blocks/%1

--------------------------

Please advise?

[edited by: jdMorgan at 5:10 am (utc) on July 21, 2007]
[edit reason] example.com [/edit]

jdMorgan

5:15 am on Jul 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mod_dir will do a redirect to add a trailing slash, because it is missing in your rule.

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule ^$ /blocks/[b]%1/[/b] [L]

Jim

AxelAG

5:27 am on Jul 21, 2007 (gmt 0)

10+ Year Member



that didnt do it either.

jdMorgan

3:47 pm on Jul 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check your scripts (if you use any) to be sure that one of them isn't doing an external redirect.

If that is not the case, then contact your host -- either they are doing a redirect for some reason, or your server is mis-configured. Redirects don't "just happen," so someone, somewhere, is doing an external redirect that is "exposing" the subdirectory to the client.

Jim