如何在 Django 应用程序中从一个域重定向到另一个域?
通常我会用 .htaccess
来做,但 django 没有.
Normally i would do it with .htaccess
but django doesn't have it.
那么从 www.olddomain.com 重定向到 www.newdomain.com 的最佳方式是什么?代码是什么?
So what is the best way and what is the code for it to redirect from www.olddomain.com to www.newdomain.com?
注意:我们使用的不是 Apache,而是 Gunicorn
NOTE: we are not using Apache, but Gunicorn
谢谢!
最好的方法仍然是使用您的 Web 服务器而不是 Django.这将比使用 Django 更快、更高效.
The best way to do this is still with your web server rather than Django. This will be much quicker and more efficient than doing it with Django.
查看这个问题 了解更多信息.
更新
如果您真的想在 django 中执行此操作,请编辑您的 url conf 文件(该文件管理 django's url dispatcher) 在顶部包含以下内容 -
If you really want to do it within django then edit your url conf file (which manages django's url dispatcher) to include the following at the top -
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
(r'^.*$', redirect_to, {'url': 'http://www.newdomain.com'}),
)
欲了解更多信息,请查看 文档.
For more info check out the documentation.