0

Is it possible to use .htaccess for a subdomain to point to a file on the main domain? I have example.com as a main domain and created subdomains: one.example.com, two.example.com, etc through DirectAdmin. The subdomains should point to app.php on the main domain but in the url the subdomain should persist.

So navigating to one.example.com should be pointing to app.php which lives in the main domain example.com, but the url should still show one.example.com

I have looked and tried a lot of the rewrite rules found on .htaccess rewrite subdomain to directory and elsewhere. Currently I have this rewrite rule in a .htaccess file in the subfolder of the subdomain:

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

This redirects and changes the url to example.com/app.php while I would like the url to show the subdomain: one.example.com/whatever

Is it possible to do this with .htaccess ,or is there maybe even a better way to do this?

Community
  • 1
  • 1
zef
  • 649
  • 1
  • 7
  • 22

1 Answers1

0

A RewriteRule with a target on another server will always do a redirect

Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL. To force an external redirect back to the current host, see the [R] flag below.


To map the URL http://example.com/app.php into your subdomains without redirecting, you might try the P|proxy flag

RewriteRule ^(.*)$ http://example.com/app.php/$1 [P,QSA]

Also keep the final note in mind

Note: mod_proxy must be enabled in order to use this flag.

Usually, this can be done by

a2enmod proxy proxy_http

If the target URL is https://..., you must also enable mod_proxy_connect, and activate SSL in your main or virtual host config with SSLProxyEngine

SSLProxyEngine on

Depending on your needs, ProxyPass might be a viable solution too.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • I tried your suggestion but it is not working, the url is still redirected to the main domain. I have the application running on example.com and want to run it on subdomains with url like one.example.com, etc. – zef Sep 19 '16 at 14:22
  • I tried this in my test environment, and it does work. See updated answer. – Olaf Dietsche Sep 20 '16 at 07:23
  • I indeed use https://... However I changed my subdomains to Domain Pointers, thus acting as alias. This is working now for me without the need for rewriting url's. I think this is a better and more easy way to do it. – zef Sep 20 '16 at 14:46