简体   繁体   中英

Apache2, re-direct blank.example.com to example.com/blank/blank

I'm having issues with this re-direct. I've tried the .htaccess method with RewriteRule and RewriteCond, as well as the VirtualDirectory method. Here's what I've tried for VirtualHost:

<VirtualHost *:80>
    ServerName blank.example.com
    Redirect permanent / "http://example.com/blank/blank"

</VirtualHost>



<VirtualHost *:80>

   ServerName example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /the/doc/root

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

and this for .htaccess:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^blank.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/blank/blank [R=301,L]

I'm not sure if it's needed or not, but blank.example.com is also pointed to localhost in /etc/hosts

Any tips on what I'm missing?

Here are a couple of things.

If you are wanting to just have a subdomain with it's own document root then your vhost needs to look like this. Then there is no need to redirect.

<VirtualHost *:80>
    ServerName blank.example.com
    DocumentRoot /path/to/blank/blank
    <Directory /path/to/blank/blank>
      #enable .htaccess for this subdomain
      AllowOverride All
    </Directory>
</VirtualHost> 

If you are truly wanting to redirect your subdomain to the main domain.

Then remove this first vhost . it's not needed at all.

 #This virtualhost is not needed
<VirtualHost *:80>
    ServerName blank.example.com
    Redirect permanent / "http://example.com/blank/blank"
</VirtualHost>

Then on your main VirtualHost add a server alias like so

ServerName example.com
ServerAlias blank.example.com
ServerAdmin webmaster@localhost
DocumentRoot /the/doc/root

<Directory /the/doc/root>
    #enable .htaccess for this main domain
   AllowOverride All
</Directory>

Then for your .htaccess rules

RewriteEngine on
RewriteCond %{HTTP_HOST} ^blank\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/blank/blank/$1 [R=301,L]

Restart Apache after all config file changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM