user2945513
user2945513

Reputation: 21

htaccess subdomain rewrite without a redirect

Using htaccess Rewrite, I want my url http://*.phoneataxi.com/ (where * is a wildcard, excluding 'www') to show in the address bar as is but get information from http://*.phoneataxi.com/test.php?c=*.

I have tried so many different things but nothing is doing exactly what I need. Most examples are redirecting the subdomain to the '/test.php' file in the address bar which I don't want to do.

I'm trying not to have to create individial subdomains and subdomain folders within my webroot.

Ideas?

Upvotes: 2

Views: 1861

Answers (2)

argon
argon

Reputation: 449

UPDATE (2020)

Some of the answers regarding this topic is very old and no longer work as expected.
After searching for hours on something that actually works, this is what I came up with; edit as you see fit:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ([a-z0-9]+)\.
RewriteRule ^(.*)$ - [E=BASE:%1]

RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}/index.php -f
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L,NC,QSA]

RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}/index.html -f
RewriteRule ^(.*)$ %{ENV:BASE}/index.html [L,NC,QSA]

Breakdown

Make sure that the rewrite module is installed and enabled on your host

  • first we turn the rewrite engine on and set the path-base
  • then isolate the subdomain - any letters/numbers before the first dot
  • set a variable in this runtime environment that contains the subdomain
  • check if the subdomain folder and index-file exists
  • if it does exist -then use that file as the request-handler (no redirect)
  • if it does not exist then the request carries on normally

Flags

The flags used here are explained here, but the ones used above are quite simple:

  • [L] Last rule, ignore the rest
  • [NC] No Case, no uppercase/lowercase restrictions
  • [QSA] I remember this as "Query String Attach" :D

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409176

I use this htaccess file to make Apache act as a proxy for another host:

IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ghost\.pileborg\.se$
RewriteRule (.*) http://vps.pileborg.se/ghost/$1 [P]
</IfModule>

It causes all access to http://ghost.pileborg.se/ to be "redirected" to http://vps.pileborg.se/ghost/.

Upvotes: 2

Related Questions