在Django中,当我转发带有屏蔽的域名后,如何让内部链接指向屏蔽的域名而不是IP地址?

发布于 2024-10-10 00:40:04 字数 354 浏览 0 评论 0原文

示例:

我在 Godaddy 上有一个域名 www.example.com,我想将其屏蔽转发到 200.200.200.200,这是托管在 amazon ec2 上的服务器。当我通过浏览器访问 www.example.com 时,我可以正常看到我的网站。但我网站上的所有链接都链接到 200.200.200.200/home。如何使链接指向 www.example.com/home?我使用 django 作为我的网络框架。谢谢!

编辑:

我使用的链接示例是 home 所以这会呈现为 home

Example:

I have a domain name on Godaddy, www.example.com, which I want to forward with masking to 200.200.200.200 which is a server hosted on amazon ec2. When I go to www.example.com through my browser, I see my site just fine. But all the links on my site link to 200.200.200.200/home. How do I make the links point to www.example.com/home instead? I'm using django as my web framework. Thanks!

edit:

an example of the linking I'm using is <a href="{% url home %}">home</a> so this gets rendered as <a href="/home/" >home</a>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖伴 2024-10-17 00:40:04

您确定需要使用屏蔽而不是转发吗?对于转发的网址,这种情况不会发生,但我想由于屏蔽只是在地址栏中粘贴了一个新网址,而实际上引用了原始页面,因此任何相对链接仍然引用原始网址。此外,据我所知,使用屏蔽会改变 Google 抓取工具的行为方式,因此您的网站可能不会在搜索结果中显示得那么高,如果这对您很重要,则需要考虑这一点。如果出于某种原因您确实需要屏蔽,我认为您必须在所有链接中使用绝对网址(GoDaddy 中可能有一些设置可以避免这种情况,但我不知道 - 如果有的话,希望其他人会回答)。

在 django 中使用绝对 url 最简单的方法可能是定义一个 ROOT_URL 变量(即 ROOT_URL = http://www.example.com)。 example.com)在settings.py中。那么您的主页链接将是:

home

您还需要传递 ' ROOT_URL'=settings.ROOT_URL 到视图的 HtmlResponse(或改为传递 context_instance),以便模板可以访问 ROOT_URL 变量。

希望有帮助!

Are you sure you need to use masking instead of forwarding? With forwarded urls this does not happen, but I guess since masking just sticks a new url in the address bar while actually referencing the original page, any relative links still refer to the original url. In addition, I understand that using masking changes the way Google's crawlers behave, so your site might not show up as high as it should in search results, which is something to look into if that is important to you. If for whatever reason you do need masking, I think you'll have to use absolute urls in all your links (it's possible there's some setting in GoDaddy to avoid this, but I have no idea - if there is, hopefully someone else will answer).

The easiest way to use absolute urls in django is probably to define a ROOT_URL variable (i.e. ROOT_URL = http://www.example.com) in settings.py. Then your home link would be:

<a href="{{ ROOT_URL }}{% url home %}">home</a>

You'll also need to pass 'ROOT_URL'=settings.ROOT_URL to the view's HtmlResponse (or pass a context_instance instead) so that the template has access to the ROOT_URL variable.

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文