-1

I'm trying to rewrite requests to subdomains to a specific file. So when I visit http://test.domain.com it shows the campaign.php

RewriteCond %{REQUEST_URI} !^index\.php
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule . /campaign.php?user=%1 [L]

This however gives me a server 500 error

If I change the last line by the following

RewriteRule . http://www.domain.com/campaign.php?user=%1 [L]

That seems to work, but it also changes the url in the browser to http://www.domain.com/campaign.php?user=test. I want to keep http://test.domain.com as the browser url and show the campaign.php file. What is the best way to achieve this?

Update:

I talked with the support desk of my hoster and they helped fixing it, this is what the script turned into:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !/(campaign|index)\.php
RewriteCond %{HTTP_HOST} ^(.*).domain\.com$ [NC]
RewriteRule (.*) campaign.php?user=%1 [L,QSA] 
Mark
  • 99
  • 2
  • 3
    It gives you 500 error because you created rewrite loop with your rule. By adding one more condition `RewriteCond %{REQUEST_URI} !^/campaign\.php` you should solve your issue. **BTW:** The very first line is **useless** as it will ALWAYS match -- the `%{REQUEST_URI}` should contain leading slash. – LazyOne Jul 27 '11 at 11:18
  • Thanks for the answer, i tried this, but it doesn't seem to solve my problem. – Mark Jul 27 '11 at 18:59
  • So .. what error (or behaviour) you currently have (when you added the line I've suggested)? If 500 error -- please check Apache error log for exact error message. – LazyOne Jul 27 '11 at 19:40
  • I'm still getting the 500 error and unfortunately I don't have access to the apache log. I almost know for sure it's cause by the /campaign.php script which is located in the root folder of the website. When I change campaign.php to the full path http://www.domain.com/campaign.php it works perfectly but the url is changed in the browser as well – Mark Jul 28 '11 at 12:38
  • The fact that it works fine when full URL is involved clearly (for me) suggests that you may have rewrite loop of some kind. That is why exact error message from error log is important. If you are on shared hosting then you **should** have access to such logs (check control panel). If it is on dedicated server ..then ask admin to get that log for you. – LazyOne Jul 28 '11 at 13:09
  • I asked the supportdesk of my hoster for the files, they couldn't give them, but they did help solve my problem. Thanks for the input! I've updated the question with the solution – Mark Jul 28 '11 at 15:15

1 Answers1

0
RewriteCond %{REQUEST_URI} !/(campaign|index)\.php
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteRule . /campaign.php?user=%1 [L]
h0tw1r3
  • 2,776
  • 19
  • 17
  • Thanks for the answer, but this doesn't seem to work :( The problem seems to be the /campaign.php file which is in the root of the website folder, so for some reason Apache can't find it – Mark Jul 27 '11 at 18:57