How to redirect URL with Apache

mod_rewrite is a powerful and sophisticated Apache web server module that allows urls to be rewritten using regular expressions. To redirect the urls to the corresponding url with www in front:

Put on on your webserver config

Centos / Fedora / RHEL: /etc/httpd/conf/httpd.conf
Debian / Ubuntu: /etc/apache2/apache2.conf

RewriteEngine On
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{HTTP_HOST}   !^(es|fr|de).
RewriteCond %{HTTP_HOST} (^[^w]{3}[^.].*)$ [NC]
RewriteRule ^.*$ http://www.%1%{REQUEST_URI} [L,NE]

Note that the second rule allows you to define urls to which the general rule will not apply, therefore all the urls that start with es., de., de. will be excluded from redirection

Later put on your VirtualHost or in the .htaccess

RewriteEngine On
RewriteOptions Inherit

Further readings

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.