0

What I want to achive is the following apache mod_rewrite:

Url -> Map to file:

*.example.com/**/* -> index.php/$1/$2

For example:

portal.example.com/ -> index.php/portal
portal.example.com/agb -> index.php/portal/agb
admin.example.com -> index.php/admin
admin.example.com/my/cool/subfolder -> index.php/admin/my/cool/subfolder

How can I reach this? Maybe something with RewriteCond?

bernhardh
  • 3,137
  • 10
  • 42
  • 77

1 Answers1

2

First, make sure that all subdomains are pointing to the same host (wildcard). This is usually done in the configuration file of the Apache host.

<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
...

Then you should get the desired redirect to work with the following configuration:

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

Reference: Redirect wildcard subdomains to subdirectory, without changing URL in address bar

Update 1:

Here is the update for you multi-wild-card scenario. Note that I changed the last line - I explicitly call the index.php of the base url, like this the output of $_SERVER['PHP_SELF'] should be /index.php/portal/index.html (or php according to your Apache configuration).

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$
RewriteRule ^(.*)/?$ http://%2.%3/index.php/%1/$1 [L,NC,QSA] 
Community
  • 1
  • 1
meberhard
  • 1,797
  • 1
  • 19
  • 24
  • Thats almost exactly what I am looking for, but one thing is missing: portal.example.com/ isn't mapped to index.php/portal – bernhardh Apr 30 '14 at 11:14
  • I just tested this on my dev-server, and it worked. Is it possible, that you tested some redirects with R=301 before, so your browser cached some "old" redirect rules? What happens, if you clear the cache or try the same with another browser (with wich you didn't test before)? – meberhard Apr 30 '14 at 11:29
  • Yes, I tested it now in IE and got the same result: If I try to access portal.example.com/ (with and without /), I get $_SERVER['PHP_SELF'] = /index.php If i access portal.example.com/agb I get $_SERVER['PHP_SELF'] = /index.php/portal/agb – bernhardh Apr 30 '14 at 11:36
  • And thats my complete code: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ RewriteRule ^(.*)/?$ index.php/%1/$1 [L,NC,QSA] – bernhardh Apr 30 '14 at 11:38
  • 1
    I updated my answer (and tested it) for your multi-wildcard scenario. – meberhard Apr 30 '14 at 12:01
  • Thank you for yor help, but that not working. But I will solve it in another way. Thank you! – bernhardh Apr 30 '14 at 12:50