.htaccessmod-rewriteamazon-ec2bitnamimod-alias

How to use .htaccess to alias/redirect subdomain to another domain sub folder


I'd like my users to go to

red.example.com

and what they actually see is a page served up from

blue.example.com/red

Is this possible using .htaccess? Essentially it's a redirect, but I don't want to have the URL change in the address bar.

Edit

Adding virtual host info.

RewriteEngine On
RewriteRule /<none> / [L,R]

<IfDefine USE_PHP_FPM>
    <Proxy "unix:/opt/bitnami/php/var/run/wordpress.sock|fcgi://wordpress-fpm" timeout=300>
    </Proxy>
</IfDefine>

<Directory "/opt/bitnami/apps/wordpress/htdocs">
    AuthType Basic
    AuthName Coincurve
    AuthUserFile "/opt/bitnami/apache2/wordpress_users"
    Require valid-user

    Options +MultiViews +FollowSymLinks
    AllowOverride None
    <IfVersion < 2.3 >
        Order allow,deny
        Allow from all
    </IfVersion>
    <IfVersion >= 2.3>
        # Require all granted
    </IfVersion>

    <IfDefine USE_PHP_FPM>
       <FilesMatch \.php$>
         SetHandler "proxy:fcgi://wordpress-fpm"
       </FilesMatch>
    </IfDefine>

    RewriteEngine On
    #RewriteBase /wordpress/
    RewriteRule ^index\.php$ - [S=1]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]

    Include "/opt/bitnami/apps/wordpress/conf/banner.conf"
</Directory>

Include "/opt/bitnami/apps/wordpress/conf/htaccess.conf"

Solution

  • I've only got one actual server at mydomain.com. the red and blue subdomains are just both pointing to that mydomain.com's IP address.

    If the subdomains are all pointing to the same place (the root of the main domain) then you probably don't need to "redirect" to a different hostname (that would involve the use of mod_proxy and configuring a reverse proxy). In other words blue.example.com/red points to the same as red.example.com/red - so it becomes a relatively simple internal rewrite.

    (Although your vhost config is incomplete, I assume you have some ServerAlias directives in there somewhere?)

    For example:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^red\.example\.com [NC]
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^ /red%{REQUEST_URI} [L]
    

    The check against the REDIRECT_STATUS environment variable is to ensure we only rewrite the initial request from the client, ie. not rewritten requests, thus avoiding a rewrite loop. REDIRECT_STATUS is empty initially and set to "200" after the first successful rewrite. Otherwise, this unconditionally rewrites everything, so it's possible to have a /red subdirectory inside the root /red directory if you wished.