Forcing www in url using Apache
To enforce www to every url, we can use mod_rewrite module in apache.
The idea is to make only one entry point to your site e.g.
1. www.techpitcher.com
2. http://techpitcher.com
3. http://techpitcher.com/index.html
4. www.techpitcher.com/index.html
As listed above there are at least 4 ways to access the home page of the site. To make one access url like http://www.techpitcher.com, we need to configure apache’s configuration file.
To use this method, you must have write access to your apache’s configuration file.
1. Open httpd.conf file. In case of Apache2, this can be a blank file.
2. Verify that mod_rewrite.so moule is loaded, Then load mod_rewrite.so module.
LoadModule rewrite_module
3. Below is piece of code which will force www in every url
<VirtualHost www.techpitcher.com:80>
DocumentRoot /home/techpitcher/public_html
ServerName www.techpitcher.com
ServerAlias techpitcher.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.techpitcher\.com
RewriteRule ^/index\.html$ http://www.techpitcher.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^techpitcher\.com
RewriteRule ^/(.*) http://www.techpitcher.com/$1 [R=301,L]
</VirtualHost>
R[=code] Redirect to new URL, with optional code. Usually you also want to stop and do the redirection immediately. To stop the rewriting you also have to provide the ‘L’ flag.
Related Post
Comments
Leave a Reply