Xampp - 将外部 url 重定向到本地主机
我使用 Xampp 安装在本地测试我的文件,然后将它们上传到我的 Web 服务器。 我的网页通常包含很多“硬编码”链接,这意味着要测试任何链接(或任何表单等),我需要更改硬编码链接以指向本地主机,然后在完成测试后重新更改它。
我正在寻找一种方法来重定向外部网址以指向我的本地主机文件夹,这样我就可以将其设置为我的网站,然后我就可以在上传它们之前进行更改。 例如,如果我在浏览器中输入“www.mysite.com/example/view.php”,我实际上会看到“localhost/example/view.php”。
这样的事情可能吗?
I use an installation of Xampp to test my files locally before I upload them to my web server. My web pages usually contain a lot of 'hardcoded' links, which means to test any links (or any forms etc) I need to change the hardcoded link to point to localhost, and then rechange it after I am done testing.
What I am looking for is a way to redirect an external url to point to my localhost folder, so I could set it as my website and then I would be able to make changes before I upload them. For example, if I were to enter 'www.mysite.com/example/view.php" in my browser, I would actually be looking at 'localhost/example/view.php'.
Is such a thing possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想到了两种解决方案:
将外部 URL 添加到主机文件中,该文件可以在以下位置找到:
C:\WINDOWS\system32\drivers\etc\hosts (Windows)
或
/etc/hosts (Linux)
只需添加这一行:
www.mysite.com 本地主机
也许你必须使用你的服务器 IP 而不是 mysite.com,我现在不确定
Two solutions come to my mind:
Add your external URL to your hosts file which can be found either in
C:\WINDOWS\system32\drivers\etc\hosts (Windows)
or
/etc/hosts (Linux)
Just add this line:
www.mysite.com localhost
Maybe you have to use your servers ip instead of mysite.com, I'm not sure right now
我只是偶然发现了这一点。
在主机中使用此行:
127.0.0.1 www.yoururl.com
I just stumbled upon this.
Use this line in hosts:
127.0.0.1 www.yoururl.com
是的:不要使用硬编码链接!
说实话,我不确定我是否理解正确。 如果您的页面中的 URL 指向同一站点上的其他页面,则应使用“/example/view.php”形式的链接,而不是“http://www.mysite.com/example/view.php"。 这样,您将始终指向同一个站点。
如果这些链接转到外部站点,但您需要在本地重定向它们以进行测试(我不明白为什么会出现这种情况,但您永远不知道......)那么您可以在配置文件中使用变量。 您只需要在一个地方更改此变量......
Yes : dont use hardcoded links !
Seriously, I'm not sure if I understood correctly. If you are talking of URLs in your pages pointing to other pages on the same site, you should use links of the form "/example/view.php" instead of "http://www.mysite.com/example/view.php". That way, you will always point to the same site.
If those links go to external sites, but you need to redirect them locally for testing (I cant see why this would be the case, but you never know ...) then you could use a varable in a config file. The you would just need to change this variable at one place ...
如果您的远程网站有静态 IP,您可以在互联网路由器上使用重定向规则。 根据需要打开和关闭规则。
捕获流向 202.20.20.20(网站 IP)的流量并重定向至 192.168.1.10(您的本地 PC IP)
此方法在 DNS 解析之后而不是之前进行重定向。 如果使用诸如 wordpress 之类的软件,这种方法以及上面的另一种方法(使用 Hosts 文件)确实非常有用; 它将开发 URL 硬编码到其数据库中。
If your remote website has a static IP, you can use a redirect rule on your internet router. Turn the rule on and off as required.
Trap traffic going to 202.20.20.20 (The website IP) and redirect to 192.168.1.10 (Your local PC IP)
This method redirects after the DNS resolution rather than before. This, and the other method above (using the Hosts file) is really useful if using software such as wordpress; which hardcodes the development URL in it's database.