Django App需要URL中的www

时间:2015-03-29 18:22:45

标签: python django apache .htaccess mod-wsgi

我最近刚使用mod_wsgi在apache服务器上启动了我的Django应用程序。如果我没有在网址中添加www,我总是会收到404错误。即“example.com”重定向到“http://www.example.com/wsgi.py”,出现404错误。

以下网址正常运行:

www.example.com/about

如果我只输入“example.com/about”,我会被重定向到www.example.com/wsgi.py/about上的404页面。

知道为什么会这样吗?我允许的主机中有“*”。我也尝试过“.example.com”。

此外,我的网站www root中有一个.htaccess文件,其中包含以下规则:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

1 个答案:

答案 0 :(得分:3)

这是使用虚拟主机的更好解决方案:

<VirtualHost *:80>
    ServerAlias example.com
    RedirectMatch permanent ^/(.*) http://www.example.com/$1
</VirtualHost>

<VirtualHost *:80>
    ServerName www.example.com
    ...your config here...
</VirtualHost>