开发者

301 Redirect subdomain to directory

开发者 https://www.devze.com 2023-02-14 17:04 出处:网络
I currently have a couple of \"shortcut\" subdomains (since some users seem to remember a subdomain more easily than an URL). I currently do this in the following way:

I currently have a couple of "shortcut" subdomains (since some users seem to remember a subdomain more easily than an URL). I currently do this in the following way:

 <VirtualHost *:80>
    ServerName redirects.domain.com
    ServerAlias *.domain.com

    RewriteEngine On

    # Option 1
    RewriteCond %{HTTP_HOST} ^option1\.domain\.com$
    RewriteRule ^.*$ http://www.domain.com/option1%{REQUEST_URI} [R=301,L]

    # Option 2
    RewriteCond %{HTTP_HOST} ^option2\.domain\.com$
    RewriteRule ^.*$ http://www.domain.开发者_运维知识库com/option2%{REQUEST_URI} [R=301,L]
 </VirtualHost>

This is the last entry in my sites-enabled folder so it won't interfere with other "real" subdomain definitions.

I have 2 questions:

  1. Could there be a general rule that just appends the subdomain part of the HTTP_HOST after domain.com/, so that 1 general rule is enough?
  2. If such a rule exists, is there still a way I can define the valid subdomains that may be redirected? (i.e. with an array-like structure)


# General Option
RewriteCond %{HTTP_HOST} ^(option1|option2)\.domain\.com$
RewriteRule ^.*$ http://www.domain.com/%1%{REQUEST_URI} [R=301,L]
0

精彩评论

暂无评论...
验证码 换一张
取 消