1

I'm failing at creating a script for my .htaccess file.

I want the following Rewrites:

  • www.domain.tld -> domain.tld (no problem)
  • admin.domain.tld/(.*) -> domain.tld/?show=admin$1
  • (everyothersubdomain).domain.tld -> domain.tld/?show=everyothersubdomain (I created a wildcard subdomain for that)

Is anyone good at this? I failed after several tries with Error 500 or simply the indexpage without any get-parameters (index.php is just print_r($_GET)).

Thanks

anubhava
  • 761,203
  • 64
  • 569
  • 643
Maddis
  • 577
  • 1
  • 6
  • 19

2 Answers2

1

I haven't tested it myself, but it should work.

RewriteCond  %{QUERY_STRING}  show=(.*)
RewriteRule  ^(index\.php|)$   http://%1.domain.tld  [R=301,L]
RewriteRule ^(admin|special1|special2)\.php    http://$1.domain.tld [R=301,L]
RewriteCond %{HTTP_HOST} ^(admin|special1|special2)\.domain\.tld$  [NC]
RewriteRule (.*) http://domain.tld/%1.php [L,NC,QSA]

RewriteCond %{HTTP_HOST}  www\.domain\.tld  [NC]
RewriteRule (.*)  http://domain.tld/$1  [L,R=301,QSA]

RewriteCond  %{HTTP_HOST}  ^(.*)\.domain\.tld$
RewriteRule (.*)  http://domain.tld/?show=%1[L,NC,QSA]

If user enters http://domain.tld/admin.php in his browser, then he will be redirected to http://admin.domain.tld. but if he enters http://admin.domain.tld, he won't be redirected externally. Let me know if this is what you want.

undone
  • 7,857
  • 4
  • 44
  • 69
  • You may want to add the `R=301` flag to the other rules. Without this the redirect occurs silently at the server end. With it, it is passed back to the client so that the client browser is aware of the redirect; it can cache it and do it automatically in future – TerryE Mar 05 '12 at 12:15
  • Well it's not exactly what I'm looking for, but a good start - thanks for that. I do want that it's silently. I might have been a bit unspecific, I will try to make a better example: the Browser shows: `admin.domain.tld` -> Serverside: `domain.tld/admin.php` or Browser: `(anyotherdomain).domain.tld` -> Server: `domain.tld/index.php?show=(anyotherdomain)` So I basicly just want to make the domains a bit more "catchier", so there is no ugly index.php?show=XXXXX, but a XXXXX.domain.tld. But the clue is that I want some "reserved" subdomains for a special script that I can define, eg. admin.php – Maddis Mar 06 '12 at 00:35
  • "Almost"! It works in the way of redirecting, the only thing that - as I said - the user can the the `domain.tld/index.php?=XXXXX` version, but I don't want them to, they should always be able to see the "subdomained version". So this does not seem to be a "rewrite" rather than a redirection. I made a few test runs but it always changes the URL to the one you mentioned in the RewriteRule. – Maddis Mar 06 '12 at 02:27
  • `301` http status code is cached in your browser, delete your cache and test again! – undone Mar 06 '12 at 02:54
  • Well it didn't work again. But this time I solved it myself. The first problem was that the first 3 lines lead to unlimeted redirection, so I commented them out (not necessary for me). After trying some other flags (proxy), I figured out that it's very simple. The http://domain.tld was the problem! so I deleted it, so that everything leads to the index.php directly! That solved it. I will put up the solution as another answer, but that wouldn't have been possible without you. Thanks! You just saved my day ;) – Maddis Mar 06 '12 at 04:35
0

Thanks to @Death I came up with this solution for myself:

RewriteCond %{HTTP_HOST} ^(admin|special1|special2)\.domain\.tld$  [NC]
RewriteRule (.*) index.php?mode=%1 [L,NC,QSA]

RewriteCond %{HTTP_HOST}  www\.domain\.tld  [NC]
RewriteRule (.*)  http://domain.tld/$1  [L,R=301,QSA]

RewriteCond  %{HTTP_HOST}  ^(.*)\.domain\.tld$
RewriteRule (.*)  index.php?show=%1 [L,NC,QSA]

I made some test runs with an index.php, which is just:

<?php print_r($_GET); ?>

And every test run worked out perfectly!

Maddis
  • 577
  • 1
  • 6
  • 19
  • Just some test results: `admin.domain.tld -> Array ( [mode] => admin )` and `asdf.domain.tld -> Array ( [show] => asdf )` – Maddis Mar 06 '12 at 05:02