Search code examples
.htaccessmod-rewritesubdomainconcrete5

htaccess wildcard subdomain with subpages


I've managed to get wildcard subdomains active on my website. However I would like to be able to create subpages and be able to go to those subpages.

for example:
example.com == domain
test.example.com == subpage on example.com/subdomain/test
test.example.com/subtest == subpage of the subdomain , I can't get this to work.

I am using concrete5 CMS...

My current htaccess code

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ^(.+).example.com
RewriteRule ^([^/]*)$ http://www.example.com/subdomain/%1 [P,L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f

RewriteRule . index.php [L]
</IfModule>

I've also tried with this code:

RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ^(.+).example.com
RewriteRule ^([^/]*)$ http://www.example.com/subdomain/%1/$1 [P,L,QSA]

However this always shows the content of the subdomain page and not the page below the subdomain.

---- EDIT ----

Croises gave me the answer in chat.

Here is my htaccess now which works with subpages of a wildcard subdomain (using Concrete5 CMS) :

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} (.+)\.example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule ^ index.php/subdomain/%1%{REQUEST_URI} [NE,L]

RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ^(.+).example.com
RewriteRule ^([^/]*)$ http://www.example.com/subdomain/%1 [P,L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f

RewriteRule . index.php [L]

Solution

  • You can use that:

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