I'm trying to find a solution to

  1. foo.bar.com (fancy url) ----> serve content from bar.com/subdom/foo/ (back-end folder)
  2. redirect (back-end folder) bar.com/subdom/foo/ ----> foo.bar.com (fancy url)

I'm trying to make the subdomains serve content from another folder, but I want visitors to be redirected to fancy URL when they navigate to the folder manually.

I suspect alias could be my saving grace, as using Rewrite creates an infinite loop (since it serves the content as another directory instead of just spoofing it).

This is the code I'm using, I highlighted the parts that create the loop for me, although without them there is no redirect back to the fancy URL.

RewriteEngine On
Options -Indexes

# Aliases
    RewriteCond %{REQUEST_URI} !^domains/
    RewriteCond %{REQUEST_URI} !^/domains/
    RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
    RewriteCond %{DOCUMENT_ROOT}/domains/%2 -d
    RewriteRule (.*) domains/%2/$1 [DPI,L]

    RewriteCond %{REQUEST_URI} ^/domains/(.*)$                      #This is problematic
    RewriteRule ^(.*) http://%1 [R=301,NC,L]                        #This is problematic

# Subdomains
    RewriteCond %{REQUEST_URI} !^subdom/
    RewriteCond %{REQUEST_URI} !^/subdom/
    RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.([^\.]*)\.([^\.]*)$
    RewriteCond %{DOCUMENT_ROOT}/subdom/%2 -d
    RewriteRule (.*) subdom/%2/$1 [DPI,L]

    RewriteCond %{REQUEST_URI} ^/subdom/(.*)$                   #This is problematic
    RewriteRule ^(.*) http://%1.%{HTTP_HOST}/ [R=301,NC,L]      #This is problematic

#################Things below should be non-relevant####################

# www to non-www (HTTP)
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/ [R=301,L]

# www to non-www (HTTPS)
    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/ [R=301,L]

# HTTP to HTTPS
#   RewriteCond %{HTTPS} on
#   RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA,L]

# HTTP to HTTPS
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA,L]

# Error 404
    ErrorDocument 404 http://%{HTTP_HOST}?Error404

# Favicon
    RewriteRule ^favicon.ico favicon.ico [L]

Thanks for any insight in advance.

0 Answers