1

I am having trouble with an htaccess setup. My goal is to have a wildcard sub directory point to the appropriate folder but without changing the URL. I followed some examples here on stackoverflow and elsewhere. I already have my wildcard setup in DNS, Virtual Host is good to go and mod packages. All that is left is the htaccess pointing to the matching folder without changing the url in the browser.

This Works as in I see the domain.com/example/index.html file (But the url changes from example.domain.com to domain.com/example in browser)

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1/$1 [L,NC,QSA]
</IfModule>

When I swap out the L flag for the P flag so that the url doesnt change. When I do and I try example.domain.com the url does not change as intended but it shows me what I would see if I had went to domain.com instead of what is in the /example folder.

Here is what I have:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1/$1 [P,NC,QSA]
</IfModule>

What am I doing wrong?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Derek
  • 4,747
  • 7
  • 44
  • 79
  • Your rule looks fine (assuming you have `mod_proxy` setup correctly). Do you have other rules in your site root .htaccess? – anubhava Oct 06 '16 at 11:27
  • Yes, WordPress is also setup in the root domain so I have that bit of code in the top of my htaccess file – Derek Oct 06 '16 at 15:59
  • 1
    In that case, move this rule just below `RewriteEngine On` line. – anubhava Oct 06 '16 at 16:03

1 Answers1

0

You are trying to provide the URL in your substitution, which causes the redirection. Try to simply set config as follows:

In VirtualHost

ServerName example.com
ServerAlias *.example.com

DocumentRoot /path/to/your/doc/root

In htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteCond %{REQUEST_URI} !^/%1 [NC]
# Please confirm whether you need the next line in your rules.
# I should think that it'd be required
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [L,QSA]
hjpotter92
  • 78,589
  • 36
  • 144
  • 183