1

I have a site called: www.example.com this is the main domain. Now I have a Alias: www.example2.com.

I want that example2.com will be forwarded to a subdomain of the main domain to subdomain.example.com.

I used following code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\?example2\.com$ [NC]
RewriteRule ^(.*)$ http://subdomain.example.com/ [R=301,L]

I got an 500 Internal Server Error. And i really don't know why.

1

As w3d suggested in the comments, you have an open parenthesis, but not one to close it

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\?sek-muenchenstein\.ch$ [NC]
# right here -------------^
RewriteRule ^(.*)$ http://muenchenstein.sek-am.ch/ [R=301,L]

Also, in the COND line, you have www\?. I think this might be wrong too, it's now literally looking for the ? in the url, like below, which is highly unlikely:

www?sek-muenchenstein.ch

If you want the alias to redirect, just test without www.
Also also, if you are at siteA.com/example you will end up at siteB.com, without /example.


Code with all suggestions:

RewriteEngine On
RewriteCond %{HTTP_HOST} sek-muenchenstein\.ch$ [NC]
RewriteRule ^(.*)$ http://muenchenstein.sek-am.ch/$1 [R=301,L]
#  Take ------^ and put it ------------------------^^

Or, if you plan ahead and there are multiple alias, reverse the check:

RewriteEngine On
# if not maindomain:
RewriteCond %{HTTP_HOST} !muenchenstein.sek-am.ch$ [NC]
RewriteRule ^(.*)$ http://muenchenstein.sek-am.ch/$1 [R=301,L]

Try to make your regexes less detailed. It's easier to read the less you add, and less prone to errors. And changes are less likely to mess things up, eg adding a subdomain. Your code will stop working because of the www, mine wont.

  • "its the dashes (-)." - A dash is only a special character when used inside a character class ([]), to indicate a range (as you say). Outside of a char class, the hyphen/dash has no special meaning and should be used as is. – MrWhite Oct 15 '14 at 15:38
  • The missing closing parenthesis on the RewriteCond pattern in the original code is likely to be the cause of the immediate 500 internal server error (although the parenthesis are unnecessary and you've quietly removed them in your later example). Also I would avoid the inline comment (in your last example) - this can also cause a 500 error (inline comment support was removed - by default - in later versions). – MrWhite Oct 15 '14 at 15:56
  • Indeed you are correct :) – Martijn Oct 15 '14 at 17:21
  • It worked out. I will say thank you a lot for your help. Asked the first time on this page, and got so a good short answer. Thank you very much! – Fabrizio Cocco Oct 16 '14 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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