How do configure htaccess to subdomains

my .htaccess is:

DirectoryIndex index.html index.php index.asp
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>

how can i put the subdomains working on this?
i just had to put this lines?

# test.domain.com ==> domain.com/program.php?var=test

RewriteEngine On
# Extract the subdomain from the domain name
RewriteCond %{HTTP_HOST} ^([^.]+)\\.example\\.com$ [NC]
# Verify that it's not the main domain, ie www
RewriteCond %1 !^www$ [NC]
# Then we are ready for the internal redirection
RewriteRule !^program.php$ /program.php?var=%1 [L]

i don´t know how to do it or where to put the .htaccess because every time i upload it he disappears…
tks in advanced

art,

First, welcome to SitePoint and the Apache forum!

Now, let me start by going through your .htaccess with some comments:

DirectoryIndex index.html index.php index.asp
[COLOR="Red"]<IfModule mod_rewrite.c>
# WHAT?  You don't KNOW whether you ahve mod_rewrite loaded???[/COLOR]
  RewriteEngine on
[COLOR="Red"]  RewriteBase /
# Where's the mod_alias Redirect that this is correcting for???[/COLOR]
  RewriteCond &#37;{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php[COLOR="Red"]/$1[/COLOR] [[COLOR="Red"]PT[/COLOR],L]
# Redirect EVERYTHING?  Bad move!
# Redirect to a file in the index.php directory? 
# That requires Options - MultiViews and is another bad thing to do!
# Why didn't you use a query string?  It would still be hidden.
# PassThrough?  You're redirecting, not passing through!
[COLOR="Red"]</IfModule> [/COLOR]

That will do it (without the ! on an Apache 2.x server which has its subdomains sharing the DocumentRoot with the main domain. IF the subdomain DocumentRoot is in a subdirectory of the main domain, its request will NEVER be processed by this .htaccess in the domain’s DocumentRoot. That begs the question: What are your DocumentRoot for the main domain and the test subdomain? If they are the same, your code will work. If not, never.

Regards,

DK

how would my htaccess be in the end?

DirectoryIndex index.html index.php index.asp
&lt;IfModule mod_rewrite.c&gt;
# WHAT?  You don't KNOW whether you ahve mod_rewrite loaded???
  RewriteEngine on
  RewriteBase /
# Where's the mod_alias Redirect that this is correcting for???
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [PT,L]
# Redirect EVERYTHING?  Bad move!
# Redirect to a file in the index.php directory?
[COLOR="Red"]# That requires Options - MultiViews and is another bad thing to do
# Why didn't you use a query string?  It would still be hidden.![/COLOR]
but in this way how would my url be? [COLOR="Cyan"]sub.site.com to site.com/index.php?sub[/COLOR]?
# PassThrough?  You're redirecting, not passing through!
&lt;/IfModule&gt;
#
# test.domain.com ==&gt; domain.com/program.php?var=test
#
RewriteEngine On
# Extract the subdomain from the domain name
RewriteCond %{HTTP_HOST} ^([^.]+)\\.example\\.com$ [NC]
# Verify that it's not the main domain, ie www
RewriteCond %1 !^www$ [NC]
# Then we are ready for the internal redirection
RewriteRule !^program.php$ /program.php?var=%1 [L] 

i haven´t understand, sorry, the What are your DocumentRoot for the main domain and the test subdomain? If they are the same, your code will work. If not, never.…could you explain that to me…sorry

and so…the htaccess would be like…
the thing that i want was sub.aeestgl.com to aeestgl.com/index.php?id=54&blog=sub

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.aeestgl\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^www\.([^.]+)\.aeestgl\.com(.*) /aeestgl/index.php=id=30&blog=$1

is it?

art,

The answers all depend upon how your server is organized.

DocumentRoot is the root directory for any domain and is where all requests for files must pass through (it’s .htaccess is read for every request).

DocumentRoot for a subdomain is the same thing but is the root directory for that subdomain. IT CAN BE ON ANOTHER SERVER but is typically assigned (by your control panel) to be in a subdirectory of the main domain’s DocumentRoot. In other words, http://example.com/subdomain/index.php is typically the SAME FILE as http://subdomain.example.com/index.php. However, if your control panel does things differently (or you have “parked” the subdomain on top of the main domain, then http://example.com/index.php will be the same file as http://subdomain.example.com/index.php.


DirectoryIndex index.html index.php index.asp
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?var=$1 [L]

#
# test.domain.com ==> domain.com/program.php?var=test
#

# Under the "standard" setup for subdomains,
# subdomain requests will bypass the main domain's
# DocumentRoot so the rest is irrelevant.

# Under the "non-standard" shared DocumentRoot scenario ...

# Extract the subdomain from the domain name
RewriteCond %{HTTP_HOST} ^([^.]+)\\.example\\.com$ [NC]
# Verify that it's not the main domain, ie www
RewriteCond %1 !^www$ [NC]
# Then we are ready for the internal redirection
RewriteRule ^/?program\\.php$ program.php?var=%1 [L] 

Under this second scenario, sub.aeestgl.com/program.php would be redirected to the program.php file with a var=sub query string; everything else which is not a file or directory would get redirected to index.php with a query string of var=garbage (garbage as collected by the dreaded :kaioken: EVERYTHING :kaioken: atom).

As for your code, you simply cannot access any Apache variable with a RewriteRule’s regex except {REQUEST_URI} so your domain name will cause a non-match every time.

For more information about mod_rewrite, I refer you to my signature’s linked tutorial which is based on YEARS of questions in this forum.

Regards,

DK

and sorry for all the sily questions :slight_smile:

art,

NO question is silly if you don’t know (for sure) the answer! I’m here to help you and hope that I’ve managed to clear away a bit of the fog.

Regards,

DK


you send me that code but it didn´t work.. i don´t understand what i have to do or put in the cpanel..

Extract the subdomain from the domain name

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
this peice of code takes off the subdomain name and is store in %1 right?
ex: www.sub.aeestgl.com it means that %1=sub in this case
in my case the line would be RewriteCond %{HTTP_HOST} ^([^.]+)\.aeestgl\.com$ [NC]

Verify that it’s not the main domain, ie www

RewriteCond %1 !^www$ [NC]
in this we will check if is the main domain sutch www.aeestgl.com
if it is it won´t do nothing..

Then we are ready for the internal redirection

RewriteRule ^/?program\.php$ program.php?var=%1 [L]
in here and if it was a subdomain it will put the variable %1 in action..
in this case it will point to www.aeestgl.com/program.php?ver=%1
or in my case, changes made of course, www.aeestgl.com/index.php?id=30&blog=%1..soo
the line would be.. RewriteRule ^/?index\.php$ index.php?id=30&var=%1 [L]

so in the end it would all be like..

# Extract the subdomain from the domain name
RewriteCond %{HTTP_HOST} ^([^.]+)\\.aeestgl\\.com$ [NC]
# Verify that it's not the main domain, ie www
RewriteCond %1 !^www$ [NC]
RewriteRule ^/?index\\.php$ index.php?id=30&var=%1 [L][/B]

but i don´t understand what to do in cpanel.. if you could check my php version in aeestgl.com/php.php i don´t know if it some problem with it or with apache.. tks in advanced..

edited:
i had being trying.. but something strange happened.. with that code every thing that i put ex: www.aeestgl.com/<anything> it goes to www.aeestgl.com.. i don´t know why..in the htaccess that isn´t mentioned..