Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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] 
share|improve this question
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 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

1 Answer 1

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]
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.