Django-从网址中删除www [英] Django - remove www from URLs

查看:123
本文介绍了Django-从网址中删除www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将其添加到我的 .htacces

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

,但随后尝试访问 www.example.com ,将我重定向到:

but then trying to reach www.example.com redirects me to:

http://example.com/example/wsgi.py/

因为我有 WSGIScriptAlias /home/www/example.com/example/wsgi.py 指令放在我的 httpd.conf 中,我当然会收到404错误。

because i have WSGIScriptAlias / home/www/example.com/example/wsgi.py directive in my httpd.conf and of course i get 404 error.

最终,我设法通过在我的 urls.py 修复 $ c>:

Eventually, i've managed to fix this by adding next line in my urls.py:

url(r'^ example / wsgi.py / $',index),(因此它会重定向到首页)

url(r'^example/wsgi.py/$', index), (so it redirects to home page)

但我不太确定这是正确的方法(因为当我尝试达到 example.com 我看到Web浏览器将地址快速更改为 www.example.com ,然后又更改为 example。 com

but i'm not quite sure that this is the right way to do it (because when i try to reach example.com i see that web browser changes address quickly to www.example.com and then again to example.com)

如果有人会问,是的,我看过,但这也无济于事,因为浏览器遇到了URL递归问题(example.com/example.com/example.com/example.com /example.com ...)

If anyone would ask, yes i've seen this but this didn't help me either, because browser gets url recursive problem (example.com/example.com/example.com/example.com/example.com...)

编辑:文件夹结构

此是我的文件夹结构:

\mysite\
静态\
媒体\
。 htaccess
manage.py
mysite\
模板
templatetags
tinymce
静态
urls.py
settigs.py
views.py
wsgi.py
模型。 py
form.py
__init __。py

推荐答案

我找到了使用中间件和使用Apache mod_rewrite config可以更轻松地完成无www重定向。

I find it much simpler to accomplish no-www redirects with middleware that with with Apache mod_rewrite config.

您链接到的中间件看起来很有效。我猜你的问题来自Apache配置-确保删除所有mod_rewrite命令( Rewrite * 东西),然后重新启动apache服务器(参考 Apache文档,但请检查您的操作系统,可能是特定的。)

The middleware that you linked to looks like it does the trick. I'm guessing your problems came from Apache config - make sure you remove all mod_rewrite commands (Rewrite* stuff) and then restart the apache server (ref. Apache docs but check for your OS, might be specific).

您应该做的另一项调整是:确保不重定向任何POST请求,因为这可能会导致数据丢失(切线ref:为什么HTTP不具有POST重定向?)。

There is only one additional tweak that you should to: make sure you don't redirect any POST requests, because that might result in lost data (tangent ref: Why doesn't HTTP have POST redirect?).

无论如何,这就是我使用的方式,对我来说效果很好:

Anyways, this is what I use, worked quite well for me:

from django.http import HttpResponseRedirect

class NoWWWRedirectMiddleware(object):
    def process_request(self, request):

    if request.method == 'GET':  # if wanna be a prefect REST citizen, consider HEAD and OPTIONS here as well
        host = request.get_host()
        if host.lower().find('www.') == 0:
            no_www_host = host[4:]
            url = request.build_absolute_uri().replace(host, no_www_host, 1)
            return HttpResponseRedirect(url)

要使用它,请将文件放在某个位置,也许是 mysite / mysite / middleware.py
然后确保它在您的 settings.py 中运行:

To use it, put in a file somewhere, maybe mysite/mysite/middleware.py. Then make sure it's run, in your settings.py:

MIDDLEWARE_CLASSES = (
    'mysite.middleware.NoWWWRedirectMiddleware',
    # ... other middleware ...

如果您的 settings.py 中没有 MIDDLEWARE_CLASSES ,则复制默认值来自在Django文档中,但是让您正在查看正确版本的Django,1.7中有一些更改!

If there is no MIDDLEWARE_CLASSES in your settings.py then copy the defaults from here in the Django docs but make you're looking at the correct version of Django, there are some changes in 1.7!

这篇关于Django-从网址中删除www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆