Mobile app version of vmapp.org
Login or Join
Kristi941

: .htaccess rewrite problem for domain I want to use a particular page in my site as any subdomain. I need something like this http://*.site.com/. So, as the user types http://user1.site.com/ or

@Kristi941

Posted in: #Htaccess

I want to use a particular page in my site as any subdomain. I need something like this *.site.com/.
So, as the user types user1.site.com/ or user2.site.com/ then the page must shown will be present in site.com/profile.php. user1.site.com/ must point to site.com/profile.php NOT REDIRECT

Can it be possible ?

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Kristi941

3 Comments

Sorted by latest first Latest Oldest Best

 

@Kristi941

While searching through out the internet, i got this link.
stackoverflow.com/questions/13875273/dynamic-subdomain-with-htaccess-not-redirect
Here is the solution i got

RewriteCond %{HTTP_HOST} !^www.domain.com

RewriteCond %{HTTP_HOST} ([^.]+).domain.com [NC]

RewriteRule ^/?$ /member.php?username=%1 [L]

RewriteCond %{HTTP_HOST} !^www.domain.com

RewriteCond %{HTTP_HOST} ([^.]+).domain.com [NC]

RewriteRule ^/?contact$ /contact.php?username=%1 [L]

RewriteCond %{HTTP_HOST} !^www.domain.com

RewriteCond %{HTTP_HOST} ([^.]+).domain.com [NC]

RewriteRule ^/?album/([a-zA-Z0-9_-]+)/([0-9]+)$ /album.php?username=%1&title=&album_id= [L]

But it is exactly not same as my requirement. I need to change this code as my need.

10% popularity Vote Up Vote Down


 

@Angie530

Wildcard VirtualHost

You can use a wildcard VirtualHost statement.

<VirtualHost *:80>
DocumentRoot /path/to/doc/root
ServerName *.domain.com
</VirtualHost>

If you have a specific host, e.g. domain.com, put its VirtualHost stanza above the wildcard one. Apache works on a first match basis.

Also, you may want to add:

DirectoryIndex profile.php

to either a .htaccess or your VirtualHost if you want that file to be served up by default when no file is specified.

This will be needed to serve profile.php when a user type *.domain.com/.
DNS

You can then setup a wildcard DNS entry for *.domain.com or use CNAME/A record for each user.

10% popularity Vote Up Vote Down


 

@Megan663

Are you talking about sharding? Look at "Advanced Apache Rewriting" - at least probably your solution will use a RewriteMap .. although you do say dynamically, I still assume your list of users is static. RewriteMap Apache Documentation should be the way to go - you put your mapping in a text file like so:

Ralf.S.Engelschall rse # Bastard Operator From Hell
Mr.Joe.Average joe # Mr. Average


Then you define this map in the .htaccess/apache-conf:

RewriteMap real-to-user txt:/path/to/file/map.txt


Then you use that mapping in a rewrite rule:

RewriteRule ^/ex/(.*) ${examplemap:}


Another way - if you want to check inside that profile.php you mentioned - would be to do the following:

RewriteEngine On
RewriteCond %{HTTP_HOST} !=site.tld
RewriteRule ^ site.tld/profile.php?host=%{HTTP_HOST}&uri=%{REQUEST_URI} [R=301,L]


You should have your setup so that your "catch all"-host has that RewriteRule, but your site.tld-host is seperate and thus doesn't run into a loop. OFC you'll end up with people accessing user1.site.tld and ending up with an address bar filled with site.tld/?was=user1.site.tld&uri=/... If you need something different it's probably possible too though.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme