mod_rewrite Fedora 13
How to configure mod_rewrite on Fedora 13
- Open the Apache configuration file
gedit /etc/httpd/conf/httpd.conf
- Change AllowOveride None to AllowOveride All inside the DocumentRoot Directory Directive, normally <Directory "/var/www/html">
- Save and close the file
- Restart the Apache service
service httpd restart
Permanently redirect users to access the site WITH or WITHOUT the 'www.' prefix
- Create/Open a .htaccess file in the document root folder and add the following text replacing variables with appropriate values where necessary
# mod_rewrite
<IfModule mod_rewrite.c>
# Enable mod_rewrite engine
RewriteEngine on
# WITH 'www.'
RewriteCond %{HTTP_HOST} ^$uri\.$tld$ [NC]
RewriteRule ^(.*)$ http://www.$domain$1 [L,R=301]
# WITOUT 'www.'
RewriteCond %{HTTP_HOST} ^www\.$uri\.$tld$ [NC]
RewriteRule ^(.*)$ http://$domain/$1 [L,R=301]
</IfModule>
- Restart the Apache daemon
service httpd restart
Notes:
- Choose either WITH or WITHOUT the 'www.' prefix. Do not include both or you will end up in a redirection loop.
- If there is already an <IfModule mod_rewrite.c> directive, only copy the desired #RewriteCond and #RewriteRule lines and add them to the directive.
- Replace http:// with https:// if used for a Virtual Hosts using the https protocol.
- This text can also be added to the VirtualHost directive in the apache configuration file, but you can't typically move these changes to a live server, so the .htaccess file is the prefered method.