Derp
Derp

Reputation: 73

htaccess rewrite subdomain to subfolder

I am trying to achieve getting subdomains pointed to subfolders so for example http://{subdomain}.example.com points to http://example.com/{subdomain} without changing the URL in the address bar.

I currently have this code:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1 [L]

Which seems to work, but it changes my URL bar aswell

I knnow there are multiple questions about this, but none fitted my awnser.

Upvotes: 1

Views: 76

Answers (2)

Azrael
Azrael

Reputation: 1094

Try using virtualhosts instead, edit your httpd-vhosts.conf and add this:

<VirtualHost *:80>
  ServerAlias *.yourdomain.com #wildcard catch all
  VirtualDocumentRoot /opt/lampp/htdocs/%1
  UseCanonicalName Off
  <Directory "opt/lampp/htdocs">
    Options FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

Upvotes: 1

anubhava
anubhava

Reputation: 785126

You can use this lookahead based rule in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^.*\.example\.com$ [NC]
RewriteCond %{HTTP_HOST}:%{REQUEST_URI} ^([^.]+)\.[^:]+:(?!/\1/).*$
RewriteRule ^ %1%{REQUEST_URI} [L]

Upvotes: 1

Related Questions