如何在 Django 下将domain.com重定向到WWW.domain.com?
如何在 django 站点中使用 301 将所有对 domain.com/... 的请求重定向到 www.domain.com/... ?
显然这不能在 urls.py 中完成,因为你只能在其中获取 URL 的路径部分。
我无法在 .htaccess 中使用 mod rewrite,因为 .htaccess 文件在 Django 下不执行任何操作(我认为)。
我猜中间件或 apache conf 中有什么东西?
我在带有 Plesk 的 Linux 服务器上运行 Django,使用 mod WSGI
How do I go about redirecting all requests for domain.com/... to www.domain.com/... with a 301 in a django site?
Obviously this can't be done in urls.py because you only get the path part of the URL in there.
I can't use mod rewrite in .htaccess, because .htaccess files do nothing under Django (I think).
I'm guessing something in middleware or apache conf?
I'm running Django on a Linux server with Plesk, using mod WSGI
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有人指出的 WebFaction 讨论就配置而言是正确的,您只需自己应用它,而不是通过控制面板。
放入 .htaccess 文件中,或放入适当上下文中的主 Apache 配置中。如果在主 Apache 配置中的 VirtualHost 内部,则 ServerName 为 www.example.com,ServerAlias 为 example.com,以确保虚拟主机处理这两个请求。
如果您无权访问任何 Apache 配置(如果需要),可以使用 Django WSGI 应用程序入口点周围的 WSGI 包装器来完成。类似于:
修复此问题以将 URL 包含在站点内并处理 https 留给读者作为练习。 :-)
The WebFaction discussion someone pointed out is correct as far as the configuration, you just have to apply it yourself rather than through a control panel.
Put in .htaccess file, or in main Apache configuration in appropriate context. If inside of a VirtualHost in main Apache configuration, your would have ServerName be www.example.com and ServerAlias be example.com to ensure that virtual host handled both requests.
If you don't have access to any Apache configuration, if need be, it can be done using a WSGI wrapper around the Django WSGI application entry point. Something like:
Fixing this up to include the URL within the site and dealing with https is left as an exercise for the reader. :-)
PREPEND_WWW 设置就是这样做的。
The PREPEND_WWW setting does just that.
有一种一种轻量级的方法来做到这一点涉及虚拟主机 和 mod_alias 重定向指令。您可以定义两个 VirtualHost,一个保存重定向,另一个保存 站点配置:
这样就可以完成任务了。
There is a lightweight way to do that involving VirtualHosts and mod_alias Redirect directive. You can define two VirtualHosts, one holding the redirect and another holding the site configuration:
And that will do the job.
这也可以通过中间件来完成。
一些示例:
这是 snippet-510 的更好版本:
This also can be done with a middleware.
Some examples:
This is a better version of snippet-510: