Thursday, August 30, 2012

Apache Virtualhost URL Rewrite - Keys to Success

Have you ever tried to make Apache URL Rewrite to work at the Virtualhost level and felt like you are banging your head on the wall, don't think you are alone, I am with you.
After lots and lots of search,  read through documentation, trial and error, I  finally got it to work. This configuration will work with a Reverse Proxy Server in the front.  Also if Apache & Reverse Proxy Servers are listening in different port (Example Reverse Proxy on port 80 and Apache on port 8080), use the Apache listening port on your VirtualHost configuration.

Always start with  "Apache -S" (In some cases httpd -S) option. This will list all the issues found in httpd.conf file. Actually I found couple of issues with my httpd.conf file, in fact Apache was up and running with those issues and driving me crazy.

For the Jump Start just copy paste the following lines into to your https.conf file and JUST change the domain name.



NameVirtualHost *:80
<VirtualHost *:80>
ServerName mydomain.com
DocumentRoot "c:\www\mydomain.com"
# Following two lines are handy for debugging, but don't do it on your production machine
RewriteLog "C:\www\temp\rewrite.log"
RewriteLogLevel 9
RewriteEngine on
# Put all your ReWrite rules over here for mydomain.com
RewriteRule index.html$ welcome.html [NC,L]
</VirtualHost>
<VirtualHost *:80>
ServerName myotherdomain.com
DocumentRoot "c:\www\myotherdomain.com"
RewriteEngine on
# Put all your ReWrite rules over here for myotherdomain.com
RewriteRule index.html$ welcome.html [NC,L]
</VirtualHost>


Now lets talk about all the mistakes you can do (To be precise I did)

Mistake #0:
Trying to debug without running "Apache -S" (In some cases httpd -S) command. You must resolve all the issues listed by that command, before you start think about why is not working.

Mistake #1:
<VirtualHost *:80> element alone is not sufficient to trigger URL Rewrite and you need NameVirtualHost *:80 prior to that.

Mistake #2:
NameVirtualHost mydomain.com:80 is incorrect. Just go with *:80

Mistake #3:
What ever you put in after NameVirtualHost should be exactly repeated in every VirtualHost element.
Each server should be specified in ServerName attribute and not in VirtualHost
For example


NameVirtualHost *:80
<VirtualHost mydomain.com:80>
</VirtualHost>

<VirtualHost myotherdomain.com:80>
</VirtualHost>


is incorrect.
Correct way to do it is


NameVirtualHost *:80
<VirtualHost *:80>
ServerName mydomain.com
</VirtualHost>

<VirtualHost *:80>
ServerName myotherdomain.com
</VirtualHost>




Mistake #4:
No port to be specified in the ServerName
For example

<VirtualHost *:80>
ServerName mydomain.com:80
</VirtualHost>
is incorrect.

Right one is

<VirtualHost *:80>
ServerName mydomain.com
</VirtualHost>

Mistake #5:
All VirtualHost element must have at least ServerName  & DocumentRoot in it. I will add "ReWriteEngine On" to that.

For Example

RewriteEngine on  # Globally Turning it on has no impact on VirtualHost

<VirtualHost *:80>
ServerName mydomain.com
DocumentRoot "c:\www\mydomain.com"
RewriteRule index.html$ welcome.html [NC,L]
</VirtualHost>


won't do any URL ReWrite.
Instead you must put that in inside the VirtualHost


<VirtualHost *:80>
ServerName mydomain.com
DocumentRoot "c:\www\mydomain.com"
RewriteEngine on  # You need it here.
RewriteRule index.html$ welcome.html [NC,L]
</VirtualHost>

Mistake #6:
This may sound sillly, but I am sure someone will do it.
Putting  "."(dot) instead of ":" (colon) between * and 80.
*.80 is incorrect
*:80 is correct.
This may happen especially if you use IP address instead of star.


Still reading my blog, ok then, let me drive you crazy.
Lets talk about Apache listening on multiple ports and settingup VirtualHosts.

Lets say your Apache is listening on Port 80 as well as 8080 and want to setup Virtual host.
Here is the right way to do it.

NameVirtualHost *:80
NameVirtualHost *:8080

<VirtualHost *:80 *:8080>
ServerName mydomain.com
DocumentRoot "c:\www\mydomain.com"
RewriteEngine on  # You need it here.
RewriteRule index.html$ welcome.html [NC,L]
</VirtualHost>

NameVirtualHost *:80 *:8080  #  IS INCORRECT

Well it took two days for me solve this problem. I would really appreciate if you drop a comment, whether it was helpful solving your problem or not. Especially if you can mention the Mistake # that would be great, of course you can be anonymous.




No comments:

Post a Comment