Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess redirect all subdomains to the same directory

I want to be able to redirect all subdomains to a folder:

RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$ 
RewriteRule (.+)$ "http://example.com/subdomains/%1" [L,P]

for example, if some visits sub1.example.com it will keep the URL but show example.com/subdomains/sub1 and if the sub1 directory does not exist, it will show example.com/404

Is this possible?

I tried the above code but its showing me:

Forbidden
You don't have permission to access /index.php on this server.

Wordpress says:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

and at the top of my htaccess file, is:

RewriteEngine On
DirectoryIndex index.php

RewriteCond %{HTTP_HOST} admin.domain.com$
RewriteRule ^(.*)$ /admin/system/$1 [L]
like image 340
charlie Avatar asked Apr 08 '16 10:04

charlie


2 Answers

Your above .htaccess would externally redirect the calls, as you use a full URL as the target.

In your question you say you want to keep the hostname, so I will assume that is the requirement.

0) Enable rewrite engine

RewriteEngine On

1) Rewriting known subdomains to their directory in /subdomains

# Rewrite known subdomains to /subdomains/{subdomain}/
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdomains [NC]
RewriteCond %{REQUEST_URI} !^/404 [NC]
RewriteRule ^(.+)$ /subdomains/%1/ [L]
  1. When we encounter a request with a subdomain,
  2. and we have not rewritten it to /subdomains
  3. and we have not rewritten it to /404
  4. then rewrite it to /subdomains/{subdomain}/

So, if the request was

http://foo.example.com/hello

the URL in the browser would stay the same, but internally be mapped to

/subdomains/foo/

2) Rewriting unknown subdomains to /404

# Rewrite missing unknown subdomains to /404/
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %{REQUEST_URI} ^/subdomains [NC]
RewriteCond %{REQUEST_URI} !^/404 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ /404/ [L]
  1. When we encounter a request with a subdomain,
  2. and we have already rewritten it to /subdomains
  3. and we have not rewritten it to /404
  4. and it is not existing as a file
  5. and it is not existing as a directory
  6. and it is not existing as a symlink
  7. then rewrite it to /404

So, if the request was

http://bar.example.com/hello

the URL in the browser would stay the same, but internally be mapped to

/subdomains/bar/

by the first RewriteRule from 1).

If /subdomains/bar/ does not exist, the second RewriteRule from 2) will kick in and internally map it to

/404/

3) Test-Environment

I actually tested all of this with exemplary code, available here: https://github.com/janpapenbrock/stackoverflow-36497197

like image 102
Jan Papenbrock Avatar answered Oct 05 '22 00:10

Jan Papenbrock


I'd say you are experiencing a permission issue. I guess your Apache server runs as apache user. Use chmod to give apache access to this path.

like image 44
Héctor Valverde Pareja Avatar answered Oct 05 '22 01:10

Héctor Valverde Pareja