0

I’ve been pulling my hair out over this for a while now. I was wondering if there was a way that I could create dynamic subdomains with .htaccess? I’m in a sandbox environment at the minute but I’m looking to do something like this:

awebsite.co.uk -> straight to index.php *.awebsite.co.uk -> straight to init.php

So far, I’ve managed to bodge it and get this done, however, I face a real problem when it comes to params.

*.adomain.co.uk/page throws a 404 or a 500. I’ve bodged it by setting a custom 404 to init.php

The problem I have is I have a rule to hide the .php from the url so adomain.co.uk/apage will display a page.php, but when I try to accomplish the above, this gets messed up. Could anyone point me in the right direction here please? I have a wildcard A record etc. But I can’t set a ServerAlias with my hosting provider.

Many thanks

UPDATE: .htaccess (current)

Options -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

ErrorDocument 404 /sandbox/init.php

Hanlding with PHP should be as easy as

$splitDomain = explode('.', $_SERVER['SERVER_NAME']);
// TODO: remove if not sandbox
$ReqPage = str_replace('/sandbox/', '', $_SERVER['REQUEST_URI']);
//$ReqPage = str_replace('/', '', $_SERVER['REQUEST_URI']);
James P
  • 101
  • 10
  • It helps to see what you have tried. You could do something like: https://stackoverflow.com/questions/6972413/htaccess-rewriterule-two-domains-using-same-server-and-directory/6972444#6972444 – Lawrence Cherone Jun 25 '19 at 20:43
  • Thanks Lawrence. Unfortunately I’ve posted this “on to go” so I didn’t have access to the source. However, it would give anyone much to think about, for the most part I’ve had to remove it all due to trying so much and it not working. I’m essentially working from scratch at this now. Thank you for the link, I’ve had a look and this only touches lightly on what I’m looking to do. You see, I need to maintain the htaccess removing the php and keeping the friendly urls. I literally spealing, want to maintain the exact system I have (friendly urls) but with virtual/dynamic subdomains too – James P Jun 25 '19 at 21:03
  • Handled solely with php. So htaccess just sends to all to a file with friendly urls and I can manage it with php – James P Jun 25 '19 at 21:03
  • Updated with the current .htaccess – James P Jun 25 '19 at 21:13

1 Answers1

1

The default behaviour is to redirect to index.php so you just need to catch non-www subdomains.

How about:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$   [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$  [NC]
RewriteRule ^ http://www.example.com/init.php  [L,R]

Or:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$   [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$  [NC]
RewriteRule ^ init.php  [L]

The first will redirect to init.php (what you asked), the second will call init.php without the redirect (what you meant).

lufc
  • 1,965
  • 2
  • 15
  • 19