Reputation: 2193
I am using apache
+ django
+ mod_wsgi
as server. My goal is to redirect non-www
url to www
url.
I have the mod_rewrite
enabled: when executing the command Module rewrite already enabled
, I have
Module rewrite already enabled
The site.conf
in sites-available
reads
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerName www.<site-name>.com
RedirectMatch ^http://<site-name>.com/(.*)$ http://www.<site-name>.com/$1
</VirtualHost>
<Directory /var/www/<site-name>/app>
AllowOverride All
Order allow,deny
Allow from all
Header set Access-Control-Allow-Origin "http://<site-name>.com"
</Directory>
The .htaccess
file is located in the /var/www/<site-name>/app
directory:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
If I put random code in .htaccess
, the site will throw 500 error
, but the redirect never works (it behaves in the following way instead):
site-name.com
will not trigger redirect;site-name.com
, clicking links (generated by reverse()
function) will not trigger redirect;site-name.com/morestuff
will trigger redirect to www.site-name.com/morestuff
.Good that I can use @Finbarr's answer to achieve what I wanted. But then I am still interested in what leads to the above redirect behavior.
Upvotes: 1
Views: 605
Reputation: 486
Personally I wouldnt modify the .htaccess file to do this when you have access to the conf file. I would create a separate virtualhost entry for the redirect and leave the main site virtualhost as normal. A typical redirect would look like this in the conf file.
<VirtualHost *:80>
ServerName www.yoursite.com
Redirect 301 / http://yoursite.com/
</VirtualHost>
Upvotes: 3