Forum Moderators: phranque

Message Too Old, No Replies

.htaccess

Htaccess subdamin to php fetch

         

Nicklorion

2:59 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



Hi there i'm triyng to write a script to forward subdomains like: test.mydomain.com to www.mydomain.com/index.php?sub=test

but some how im doing somthing wrong
i tried this in the .htaccess:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\..+\..*$ [NC]
RewriteCond /home/httpd/vhosts/mydomain.com/httpdocs/%1!-d
RewriteCond /home/httpd/vhosts/mydomain.com/httpdocs/%1!-l
RewriteRule ^(.*)$ [mydomain.com...]

but this returns an internal server error 500
my provider said that the apache server is setup write
but some how i cant get it to work.

does any body a have an id?

Caterham

3:10 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



You cannot access backreferences within a RewriteCondition

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9]+)\..+\..*$ [NC]
RewriteRule ^.* - [E=host:%2]
RewriteCond /home/httpd/vhosts/mydomain.com/httpdocs/%{ENV:host} !-d
RewriteCond /home/httpd/vhosts/mydomain.com/httpdocs/%{ENV:host} !-l
RewriteRule ^(.*)$ http://www.mydomain.com/index.php?sub=%{ENV:host} [R=301,L]

Should a redirect take place? If yes, you should use the R=301 flag forcing a 301 redirect.
What should happen if www.sub.domain.com is called? Should the variable sub have the value www or sub?

Bob

Nicklorion

3:21 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



yes ideed a redirect should take place
i'm building an website with user profiles and the sub domain is an profile so...

the sub domain shoul be redirected to the profile of the user..

how should the new htaccess look like when using
R=301 flag forcing a 301 redirect.

and "www" should be the main http;//www.domain.com/index.php site and everything esle should be www.domain.com/index.php?sub=%1

know what i mean?

Caterham

4:13 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



how should the new htaccess look like when using
R=301 flag forcing a 301 redirect.

You'll find it in the code posted above... Have you already tested it?

Nicklorion

4:25 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



yeah i did test it but then i can only visit the site
using no sub or www.

when i use a sub it returns an empty page
and when i use www it returns a loop and keeps refreshing.
:S

i dont get it..

Caterham

5:55 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



ok, have a try on this code:

RewriteEngine on
# host is not www.domain.com
RewriteCond %{HTTP_HOST}!www\.([a-z0-9@_-]+)\.[a-z]{2,6} [NC]
#host is either www.sub.domain.com or sub.domain.com
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9]+)\.([a-z0-9@_-]+)\.[a-z]{2,6} [NC]
#set ENV
RewriteRule ^.* - [E=host:%2]
#if ENV:host exsists
RewriteCond %{ENV:host} .
#test, if string is not a directory
RewriteCond /home/httpd/vhosts/mydomain.com/httpdocs/%{ENV:host} !-d
or a symbolic link
RewriteCond /home/httpd/vhosts/mydomain.com/httpdocs/%{ENV:host} !-l
#if yes, force external redirect for the root request to...
RewriteRule ^$ http://www.mydomain.com/index.php?sub=%{ENV:host} [R=301,L]

Nicklorion

7:49 am on Jan 24, 2005 (gmt 0)

10+ Year Member



I tried this script it doesn't return an error
but it also doesnt forward to the specified user
when using test.domain.com it just views test.domain.com

and then it previews the main site.

RewriteEngine on
RewriteCond %{HTTP_HOST}!www\.([a-z0-9@_-]+)\.[a-z]{2,6} [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9]+)\.([a-z0-9@_-]+)\.[a-z]{2,6} [NC]
RewriteRule ^.* - [E=host:%2]
RewriteCond %{ENV:host} .
RewriteCond /home/httpd/vhosts/b2control.com/httpdocs/%{ENV:host}!-d
RewriteCond /home/httpd/vhosts/b2control.com/httpdocs/%{ENV:host}!-l

when i change this line instead of index using a different file like users.php
RewriteRule ^$ [b2control.com...] [R=301,L]

it also previews the index.php :S

do you have an other id?

Caterham

10:44 am on Jan 24, 2005 (gmt 0)

10+ Year Member



are you sure that /home/httpd/vhosts/b2control.com/httpdocs/sub1 is your correct full physical filesystem path for sub1.domain.com and the rules are in a place where all requests would be able "to see" the rules? The forum also cuts spaces right to a '}'. Make sure that there is a space following a }

jdMorgan

7:24 pm on Jan 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> You cannot access backreferences within a RewriteCondition

This is only partially true. You cannot access backreferences in the pattern of a RewriteCond, that is, in the right-hand side of the line. But in the left side, you can back-reference the previous RewriteCond's variables using %1 through %9, and access the following RewriteRule's variables using $1 through $9. The same is true for accessing server environment variables.

The code above can be significantly simplified by taking advantage of this and eliminating unnecessary complexities.


DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteRule .* http://www.mydomain.com/index.php?sub=%1 [R=301,L]

Also, make sure you understand the difference between an external redirect (which involves the client) and an internal rewrite (which is a local file substitution). They are different. I believe your application is normally done with an internal rewrite, so that the user does not see his address bar change. In that case, the final line above becomes:

RewriteRule .* /index.php?sub=%1 [L]

I'm not sure why you are testing for "subdomain name does not exist as link or directory". If this function is really necessary, I'd suggest doing the check something like this:

RewriteCond %{DOCUMENT_ROOT}%1 !-d

Either way, I suggest you get a simple version working first, and then add exclusions as needed.

And going all the way back to the beginning of this thread, when you get the server error, what does your error log say?

Jim

Nicklorion

10:03 am on Jan 25, 2005 (gmt 0)

10+ Year Member



> are you sure that /home/httpd/vhosts/b2control.com/httpdocs/sub1 is your correct full physical filesystem path for sub1.domain.com and the rules
> are in a place where all requests would be able "to see" the rules?
> The forum also cuts spaces right to a '}'. Make sure that there is a space following a }

Yeah the path is correct checked it several times.

jdMorgan,

Thanx will check it right away.

and im unable to check the apache log file :(

unfortunatly its the server of an provider

Nicklorion

10:08 am on Jan 25, 2005 (gmt 0)

10+ Year Member



DUDES!

this works thanx

thanx a million.

Nicklorion

2:33 pm on Jan 25, 2005 (gmt 0)

10+ Year Member



okay now that this is done i have another question.

how can i set the htaccess so that the subdomain
stay's visible?

like when someone visits [nicklorion.b2control.com...]
the htaccess forwards it to [b2control.com...]

how can i make it so that shows [nicklorion.b2control.com...]

?

jdMorgan

3:40 pm on Jan 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Already answered above... :)

Jim

Nicklorion

11:53 pm on Jan 25, 2005 (gmt 0)

10+ Year Member



you mean: RewriteCond %{DOCUMENT_ROOT}%1!-d ?

RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteRule .* /index.php?sub=%1 [L]

this doesn't work unfortunatly

do you have an alternative?

jdMorgan

10:51 pm on Jan 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe it's looping.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteRule .* /index.php?sub=%1 [L]

Jim