How to create fake sub-domains on linux ?

Hello,

I have a Drupal website that has contents with such URLs:

example.com/cars/sedan/item5&page=2

I want to make fake subdomains like this:

cars.example.com/sedan/item5&page=2

my subdomains are limited and everything in the site wont be translated as a subdomain. for example, the following URL will stay intact:

example.com/help/5
i.e. help.example.com/... should not be translate to URL above.

any inputs is greatly appreciated.
LVL 4
MichaelProAsked:
Who is Participating?
 
Jan SpringerConnect With a Mentor Commented:
You create the appropriate records in DNS:

www.example.com.     IN      A                  192.168.1.33
cars.example.com.       IN     CNAME       www.example.com.

In Apache, you'll use virtual hosts:

<VirtualHost 192.168.1.33:80>
DocumentRoot /var/www/html
ServerName www.example.com
<Directory "/var/www/html">
DirectoryIndex index.html index.php
Options ExecCGI FollowSymLinks
</Directory>
</VirtualHost>

<VirtualHost 192.168.1.33:80>
DocumentRoot /var/www/html/cars
ServerName cars.example.com
<Directory "/var/www/html/cars">
DirectoryIndex index.html index.php
Options ExecCGI FollowSymLinks
</Directory>
</VirtualHost>

0
 
MichaelProAuthor Commented:
Is there a way to do this via htaccess? because using the above method, i would have to create entries manually, and i might as well just create sub-domains.
0
 
Jan SpringerCommented:
This is a subdomain:

cars.example.com/sedan/item5&page=2

The first action that will take place is the DNS lookup of 'cars.example.com'
0
 
sinisazgConnect With a Mentor Commented:
if you just want to enable that any wildcard subdomain (*.example.com) routes to the same web directory, in the definition of virtualhost just add

ServerAlias *.example.com

of course after adding the  DNS record as the fellow said above.

However, if you want to add automatic different routing so that you have just one virtual hosts and all other "hosts" are added simply by adding a line into a config file you need to use mod_rewrite. here an example (part of virtualhost configuration):

    RewriteEngine On
    RewriteMap subdomains txt:/etc/virtualhosting/subdomain/site16.map
    RewriteMap subdirectory_cgi txt:/etc/virtualhosting/subdomain/site16.cgisubmap
    RewriteMap scriptalias_cgi txt:/etc/virtualhosting/subdomain/site16.cgimap
    RewriteMap pub_subdomain_cgi txt:/etc/virtualhosting/subdomain/site16.pubcgimap
 
    # Avoiding CGI directory spoofs at domain level -- cannot let http://domain.com/sub/cgi-bin/foo.cgi source
    #
    # multiple slash problem
    RewriteCond %{REQUEST_URI} ^[^?]*//.*$
    RewriteRule ^([^?]*)//(.*)$ $1/$2 [R,L]
    RewriteCond ${pub_subdomain_cgi:$1} ^(.+)$
    RewriteRule ^/(.*/cgi-bin)/(.+)$ %1/$2 [T=application/x-httpd-cgi]
 
    # Per directory CGI access - execute CGI scripts from within
    # ${documentroot}/cgi-bin/ -- ALLOWING FOR WILDCARDS
    RewriteCond %{REQUEST_URI} ^/cgi-bin/
    RewriteCond ${subdirectory_cgi:%{HTTP_HOST}\:*} ^(.+)$
    RewriteCond ${subdomains:%{HTTP_HOST}} ^(.+)$
    RewriteRule ^/cgi-bin/(.*)$ %1/html/cgi-bin/$1 [T=application/x-httpd-cgi,L]
 
    # Per directory CGI access - execute CGI scripts from within
    # ${documentroot}/cgi-bin/
    RewriteCond %{REQUEST_URI} ^/cgi-bin/
    RewriteCond %{REQUEST_URI} \.([^\./]+)$
    RewriteCond ${subdirectory_cgi:%{HTTP_HOST}\:%1} ^(.+)$
    RewriteCond ${subdomains:%{HTTP_HOST}} ^(.+)$
    RewriteRule ^/cgi-bin/(.*)$ %1/html/cgi-bin/$1 [T=application/x-httpd-cgi,L]
 
    # CGI script execution from the scriptalias directory
    RewriteCond %{REQUEST_URI} \.([^\./]+)$
    RewriteCond ${scriptalias_cgi:%{HTTP_HOST}\:%1} ^(.+)$
    RewriteCond ${subdomains:%{HTTP_HOST}} ^(.+)$
    RewriteRule ^/(.*)$ %1/html/$1 [T=application/x-httpd-cgi,L]
 
    # To emulate mod_rewrite
    RewriteCond ${subdomains:%{HTTP_HOST}} ^(.+)$
    RewriteCond %1/html/%{REQUEST_FILENAME} -d
    RewriteRule ^(.+[^/])$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R] [L]
 
    # Lastly, the fallback document root
    # (Protect the interpreters directory)
    RewriteCond ${subdomains:%{HTTP_HOST}} ^(.+)$
    RewriteCond %{REQUEST_URI} !/interpreters/[-0-9a-zA-Z]+-script
    RewriteRule ^/(.*)$ %1/html/$1
 

The site16.sub file contains these:
shop.vexample.hr    /home/virtual/site16/fst/var/subdomain/shop
service.example.hr    /home/virtual/site16/fst/var/subdomain/service

site16.cgisubmap:

www.shop.virtua.hr:*    *
shop.virtua.hr:*    *

other files are empty.

0
 
sinisazgCommented:
site16.cgisubmap should be:)

service.example.hr:*    *
shop.example.hr:*    *

sorry took it from myoriginal server configuration, and forgot to edit that one.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.