Pedro
Pedro

Reputation: 77

Get subdomain name with .htaccess file

I'm working in a site where each user has own subdomain. And i need that user can only see some images, depending on the subdomain name. So for example: user1.domain.com should get images inside a folder root/images/user1/ and user2.domain.com should get images inside a folder root/images/user2/

I use similar like this:

# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymLinks -MultiViews
Options +SymLinksIfOwnerMatch -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^temp\.domain\.com$ [NC]
RewriteRule ^$ /logo-base.png [L]

RewriteCond %{DOCUMENT_ROOT}/access_temp_files/$1 -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /access_temp_files/$1 [L]

RewriteCond %{DOCUMENT_ROOT}/access_temp_files/$1 !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /pic-base.png [L]

in this case when url is temp.domain.com/image.png it gets the image at root/access_temp_files/image.png and if do not exists print pic-base.png at root/

So i need the same thing but i need to get the subdomain name and use it in diferent places, for example replacing "temp" and "access_temp_files", like this:

# replacing "temp" with subdomain name in: RewriteCond %{HTTP_HOST} ^temp\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(subdomainname)\.domain\.com$ [NC]

# replacing "access_temp_files" with subdomain name in: RewriteCond %{DOCUMENT_ROOT}/access_temp_files/$1 -f
RewriteCond %{DOCUMENT_ROOT}/(subdomainname)/$1 -f

How can i get subdomainname in .htaccess file?

I hope that is not too confusing to understand :)

Thank you.

Upvotes: 0

Views: 374

Answers (2)

Alex W
Alex W

Reputation: 38183

I know you are asking about how to do this with .htaccess but I, personally, would create that functionality with virtual hosts. That way, you can specify the document root for a particular subdomain. Then you could put a separate .htaccess in each subdomain's folder or you can put a global one in the parent folder of the subdomain folders.

<VirtualHost *:80>
DocumentRoot /root/images/user1/
ServerName user1.domain.com

# Other directives here

</VirtualHost>

<VirtualHost *:80>
DocumentRoot /root/images/user2/
ServerName user2.domain.com

# Other directives here

</VirtualHost>

Upvotes: 0

anubhava
anubhava

Reputation: 785098

You can use these rules:

# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymLinks -MultiViews
Options +SymLinksIfOwnerMatch -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^temp\.domain\.com$ [NC]
RewriteRule ^$ /logo-base.png [L]

RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteCond %{DOCUMENT_ROOT}/%1/$1 -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /%1/$1 [L]

RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteCond %{DOCUMENT_ROOT}/%1/$1 !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /pic-base.png [L]

Upvotes: 1

Related Questions