Linux – Apache mod_rewrite 500 Internal Server Error

apache-2.2linuxmod-rewrite

I am trying to create a dynamic subdomain and I am having great difficulty, I keep getting 500 Internal Server Error

So for example the actual domain should be

http://www.parameterToBePassed.domain.com/

which should point/redirect

http://www.domain.com/shopping/gatename/parameterToBePassed

However when I access this URL I get an Internal Server Error (500), any ideas?

The vhost.conf holds

ServerAlias www.domain.com domain.com *.domain.com

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]

RewriteRule (.*) http://www.domain.com/shopping/gatename/$1 [L]
</IfModule>

I am using, Redhat Linux, With Plesk

Best Answer

(I have answered a post like that not so long ago, although here the situation differs a little)

Say someone goes to a.domain.com. This matches both conditions.

The main problem: your rewrite rule matches an empty input...

So, a.domain.com goes through the rule and becomes a.domain.com/shopping/gatename/.

And then it goes back to the server again, and matches again both conditions. The URL becomes a.domain.com/shopping/gatename/shoppin/gatename/.

Have you noticed? It substitute with the value of the request, not what you captured in your second RewriteCond, and there lies your other problem.

You can reuse groups captured by RewriteCond but they are preceded with %, not $. Hence, what you want really is this (with full regex metacharacters etc plus anchors):

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
# Non capturing group for "www." if present: it is of no interest
RewriteCond %{HTTP_HOST} ^(?:www\.)([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule (.*) http://www.domain.com/shopping/gatename/%1/$1 [L] # Note the %!