Dynamic subdomains and non www to www

Discussion in 'Tips/Tricks/Mods' started by tio289, May 15, 2009.

  1. tio289

    tio289 Member

    UPDATED
    hello I edit some files to do this:

    first you must edit /usr/local/ispconfig/server/plugins-available/apache2_plugin.inc.php

    edit this lines
    Code:
    ...
    if(count($rewrite_rules) > 0) {
    			$tpl->setVar('rewrite_enabled',1);
    		} else {
    			$tpl->setVar('rewrite_enabled',0);
    		}
    ...
    
    to this
    Code:
    ...
    if(count($rewrite_rules) > 0) {
    			$tpl->setVar('rewrite_enabled',1);
    		} else {
    			$tpl->setVar('rewrite_enabled',0);
    		}
    $tpl->setVar('rewrite_domain',str_replace('.', '\.', $data['new']['domain']));
    ...
    
    then change file /usr/local/ispconfig/server/conf/vhost.conf.master

    change this
    Code:
    ...
    <tmpl_if name="rewrite_enabled">
        
        RewriteEngine on
    <tmpl_loop name="redirects">
        RewriteCond %{HTTP_HOST}   ^<tmpl_var name='rewrite_domain'> [NC]
        RewriteRule   ^/(.*)$ <tmpl_var name='rewrite_target'>$1  <tmpl_var name='rewrite_type'>
    </tmpl_loop>
    </tmpl_if>
    ...
    
    (we remove IF)
    to this in both virtual hosts (80 and 443 too)
    Code:
        <IfModule mod_rewrite.c>
        	RewriteEngine on
    
    	RewriteCond %{http_host} ^<tmpl_var name='rewrite_domain'> [nc]
    	RewriteRule ^(.*)$ http://www.<tmpl_var name='domain'>$1 [r=301,nc]
    
            RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.<tmpl_var name='rewrite_domain'> [nc]
            RewriteRule ^(.*)$ http://%1.<tmpl_var name='domain'>$1 [r=301,nc]
    
        	RewriteCond %{REQUEST_URI} !^/sd_ 
    	RewriteCond %{HTTP_HOST} !^www\.<tmpl_var name='rewrite_domain'> [NC] 
    	RewriteCond %{HTTP_HOST} ^([^.]+)\.<tmpl_var name='rewrite_domain'> 
    	RewriteRule (.*) /sd_%1/$1 [L]
    
          <tmpl_loop name="redirects">
            RewriteCond %{HTTP_HOST}   ^<tmpl_var name='rewrite_domain'> [NC]
            RewriteRule   ^/(.*)$ <tmpl_var name='rewrite_target'>$1  <tmpl_var name='rewrite_type'>
          </tmpl_loop>
        </IfModule>
    
    in virtual host 443 change http:// to https://

    now domain.com will be redirected (301) to www.domain.com

    and if in domain folder create folder sd_example will be accessible on example.domain.com and www.example.domain.com will be redirected (301) to example.domain.com

    IMPORTANT!!!
    For correct function you must change the option Auto-subdomain to * in website configuration

    There are my files

    http://www.free-lance.sk/apache2_plugin.inc.php.txt

    http://www.free-lance.sk/vhost.conf.master.txt
     
    Last edited: Nov 26, 2009
  2. enmi

    enmi New Member

    Hi the configuration files im opening are not looking exactly as your howto.. Have there been any changes?
     
  3. Niels

    Niels New Member

    I have just tried and it redirected to www.example.domain.com
     
  4. enmi

    enmi New Member

    can you show me your apache2_plugin.inc.php and vhost.conf.master files?
     
  5. enmi

    enmi New Member

    this is my edits and its not working...

    apache2_plugin.inc.php:

    Code:
       if(count($rewrite_rules) > 0) {
                            $tpl->setVar('rewrite_enabled',1);
                    } else {
                            $tpl->setVar('rewrite_enabled',0);
                    }
                    $tpl->setVar('rewrite_domain',str_replace('.', '\.', $data['new']['domain']));
                                    
                    $tpl->setLoop('redirects',$rewrite_rules);
    
    and the vhost.conf.master:

    Code:
    <VirtualHost <tmpl_var name='ip_address'>:80>
    
        <IfModule mod_rewrite.c>
            RewriteEngine on
    
            RewriteCond %{http_host} ^<tmpl_var name='rewrite_domain'> [nc]
            RewriteRule ^(.*)$ http://www.<tmpl_var name='domain'>$1 [r=301,nc]
    
            RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.<tmpl_var name='rewrite_domain'> [nc]
            RewriteRule ^(.*)$ http://%1.<tmpl_var name='domain'>$1 [r=301,nc]
    
            RewriteCond %{REQUEST_URI} !^/sd_
            RewriteCond %{HTTP_HOST} !^www\.<tmpl_var name='rewrite_domain'> [NC]
            RewriteCond %{HTTP_HOST} ^([^.]+)\.<tmpl_var name='rewrite_domain'>
            RewriteRule (.*) /sd_%1/$1 [L]
        </IfModule>
    
    <tmpl_if name='php' op='==' value='suphp'>
        DocumentRoot <tmpl_var name='web_document_root'>
    </tmpl_else>
      <tmpl_if name='php' op='==' value='cgi'>
        DocumentRoot <tmpl_var name='web_document_root'>
      </tmpl_else>
        DocumentRoot <tmpl_var name='web_document_root_www'>
    
    .......
    
    # SSL Vhost
    ###########################################################
    
    <VirtualHost <tmpl_var name='ip_address'>:443>
    
        <IfModule mod_rewrite.c>
            RewriteEngine on
    
            RewriteCond %{http_host} ^<tmpl_var name='rewrite_domain'> [nc]
            RewriteRule ^(.*)$ https://www.<tmpl_var name='domain'>$1 [r=301,nc]
    
            RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.<tmpl_var name='rewrite_domain'> [nc]
            RewriteRule ^(.*)$ https://%1.<tmpl_var name='domain'>$1 [r=301,nc]
    
            RewriteCond %{REQUEST_URI} !^/sd_
            RewriteCond %{HTTP_HOST} !^www\.<tmpl_var name='rewrite_domain'> [NC]
            RewriteCond %{HTTP_HOST} ^([^.]+)\.<tmpl_var name='rewrite_domain'>
            RewriteRule (.*) /sd_%1/$1 [L]
        </IfModule>
    
    <tmpl_if name='php' op='==' value='suphp'>
        DocumentRoot <tmpl_var name='web_document_root'>
    </tmpl_else>
      <tmpl_if name='php' op='==' value='cgi'>
        DocumentRoot <tmpl_var name='web_document_root'>
    
     
  6. enmi

    enmi New Member

    scoutlan.me.vhost

    Code:
    <Directory /var/www/scoutlan.me>
        AllowOverride None
        Order Deny,Allow
        Deny from all
    </Directory>
    
    <VirtualHost *:80>
    
        <IfModule mod_rewrite.c>
        	RewriteEngine on
    
    	RewriteCond %{http_host} ^scoutlan\.me [nc]
    	RewriteRule ^(.*)$ http://www.scoutlan.me$1 [r=301,nc]
    
            RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.scoutlan\.me [nc]
            RewriteRule ^(.*)$ http://%1.scoutlan.me$1 [r=301,nc]
    
        	RewriteCond %{REQUEST_URI} !^/sd_ 
    	RewriteCond %{HTTP_HOST} !^www\.scoutlan\.me [NC] 
    	RewriteCond %{HTTP_HOST} ^([^.]+)\.scoutlan\.me 
    	RewriteRule (.*) /sd_%1/$1 [L]
        </IfModule>
    
          DocumentRoot /var/www/scoutlan.me/web
      
        ServerName scoutlan.me
        ServerAlias *.scoutlan.me
        ServerAdmin [email protected]
    
        ErrorLog /var/log/ispconfig/httpd/scoutlan.me/error.log
    
        ErrorDocument 400 /error/400.html
        ErrorDocument 401 /error/401.html
        ErrorDocument 403 /error/403.html
        ErrorDocument 404 /error/404.html
        ErrorDocument 405 /error/405.html
        ErrorDocument 500 /error/500.html
        ErrorDocument 503 /error/503.html
    	
        <Directory /var/www/scoutlan.me/web>
            Options FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
            
            # ssi enabled
            AddType text/html .shtml
            AddOutputFilter INCLUDES .shtml
            Options +Includes
        </Directory>
        <Directory /var/www/clients/client5/web7/web>
            Options FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
            
            # ssi enabled
            AddType text/html .shtml
            AddOutputFilter INCLUDES .shtml
            Options +Includes
        </Directory>
    
        # cgi enabled
    	<Directory /var/www/clients/client5/web7/cgi-bin>
          Order allow,deny
          Allow from all
        </Directory>
        ScriptAlias  /cgi-bin/ /var/www/clients/client5/web7/cgi-bin/
        AddHandler cgi-script .cgi
        AddHandler cgi-script .pl
        # suexec enabled
        SuexecUserGroup web7 client5
        # php as fast-cgi enabled
        <Directory /var/www/scoutlan.me/web>
            AddHandler fcgid-script .php .php3 .php4 .php5
            FCGIWrapper /var/www/php-fcgi-scripts/web7/.php-fcgi-starter .php
            Options +ExecCGI
            AllowOverride all
            Order allow,deny
            Allow from all
        </Directory>
    
    
    
    </VirtualHost>
    
    
    
     
  7. tio289

    tio289 Member

    I dont try this domain, because is not registred

    but place code on bottom, after this

    Code:
    <Directory /var/www/clients/client5/web7/web>
            Options FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
            
            # ssi enabled
            AddType text/html .shtml
            AddOutputFilter INCLUDES .shtml
            Options +Includes
        </Directory>
    
    AllowOverride All is important option.
     
  8. blaster667

    blaster667 New Member

    Hi.

    This feature is included in ISPConfig 3.0.1.3. To redirect HTTP queries from yourdomain.com to www.yourdomain.com change the option "Auto-subdomain" to "*" in website configuration.

    Max
     
  9. enmi

    enmi New Member

    would that work for folders also?

    like for example "yourdomain.com/test"

    Would also be available at "test.yourdomain.com" because thats the function im really interested in.
     
  10. tio289

    tio289 Member

    Yes, I forgot. This option you must select to "*".

    ENMI:
    yourdomain.com/sd_test will be available at "test.yourdomain.com", but you can edit rewriterules to remove sd_ prefix.
     
  11. enmi

    enmi New Member

    Hi! i finally got this to work. The main problem i had was the fact that i was using the ispconfig server as a name server for my clients. so outside my LAN it works but only inside if i remove the server from the dns-settings.
     
  12. Nicram

    Nicram Member

    There is 3.0.2 around now.
    Is there any simple way to make website auto redirecting from example.com to www.example.com ? Without editing any files?
    I try to use web alias, or selecting (in website domain) "*" as automatic subdomain or "www" but it did not transfer from example.com to www.example.com. How to do that?
     
  13. till

    till Super Moderator Staff Member ISPConfig Developer

    There is no need to edit any files, neither in 3.0.2 nor any version before that. Just add the following lines in the apache directives field of the website were you want to get this automatic rewrite:

    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST}//s%{HTTPS} ^[^w][^w][^w][^.].*//((s)on¦s.*) [NC]
    RewriteRule ^ http%2://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    See here for details:

    http://www.webmasterworld.com/forum92/5253.htm
     
  14. Nicram

    Nicram Member

    Thanks :) Too bad no such option in ispconfig :)
    Uh i was happy too fast. While it is loading website from www.example.com the address do not change in browser address bar.
    But well i figured it out that wordpress do it automatically if i use www.example.com as domain in it's configuration, so will try to check how it is doing that, and maybe i will be able to do same with my sites :)
     
    Last edited: Mar 21, 2010

Share This Page