Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess - Redirect subdomain to folder

This question has probably been asked for over a thousand times, but I've tried so many scripts, and googled so long while finding nothing, I thought, let's just ask.

I simply want m.daltonempire.nl to be redirected to daltonempire.nl/m/ without the user seeing the URL change.

So if m.daltonempire.nl/hello.php is requested, I want the user to keep seeing this URL, while the page given is actually daltonempire.nl/m/hello.php.

Note: I do not want www., so simply http://m.daltonempire.nl

Thanks in advance,

Isaiah v. Hunen

like image 292
Isaiah Avatar asked Oct 29 '13 16:10

Isaiah


People also ask

How do I redirect a directory to a subdomain?

Enter the URL of the Directory to redirect from a Subdomain Add in the field the full URL of the directory (for example: https://www.aleydasolis.com/blog/) where you want to redirect from a subdomain (for example: http://blog.aleydasolis.com/).

Can you redirect a subdomain?

Under Modify a Subdomain, locate the domain you want to redirect, then click its Manage Redirection link on the right. In the text box, type the URL you would like visitors to be redirected to if they go to the subdomain sample1.hgexample.com. Click Save. You will see the redirected URL under the Redirection column.

Can you redirect a subdirectory?

Sometimes you may need to redirect to subfolder when you move your website's root folder contents to a new subdirectory. You can easily redirect to subdirectory using . htaccess file in Apache web server.


2 Answers

Add this to your .htaccess in your web root / directory

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^m\.daltonempire\.nl$ [NC]
RewriteCond %{REQUEST_URI} !^/m(/|$) [NC]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ m/$1 [L]

The %{REQUEST_FILENAME} conditions would let you access /exists.php and not rewrite it to /m/exists.php. Remove those two if you want to rewrite even if that may potentially override existing files and directories.

like image 114
Ravi K Thapliyal Avatar answered Sep 21 '22 02:09

Ravi K Thapliyal


Try this example:

RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$ 
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule (.+)$ "http://example.com/%1" [L,P]

Any requests http://test.example.com will be mapped to http://example.com/test/...

Try googling dynamic subdomain with php and htaccess to get better search results.

like image 42
Latheesan Avatar answered Sep 23 '22 02:09

Latheesan