【问题标题】:redirect GET requests for absolute paths of a particular domain to another domain将特定域的绝对路径的 GET 请求重定向到另一个域
【发布时间】:2014-09-06 05:52:54
【问题描述】:

我在html中添加了一些绝对路径,即GET请求到其他域。

<script src="http://www.example.com/dir/file.js"></script>
<script src="http://www.sample.com/js/script.js"></script>
<link rel="stylesheet" type="text/css" href="http://www.example.com/all/home/style.css" />
<link rel="stylesheet" type="text/css" href="mystyle.css" />

如何将所有对 example.com 的请求重定向到 localhost, 127 0 0 1 那是 http://www.example.com/ 应该被重定向到 localhost/

不应影响其他请求

以下代码适用于重定向以 dir/ 开头的路径,但当我使用绝对域名 http://www.example.com/ 而不是 dir 链接文件时不起作用

RewriteRule ^dir/(.*)$   http://localhost/dev/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !/http://localhost/dev/.*

如何使用 htaccess 做到这一点?

【问题讨论】:

    标签: apache .htaccess redirect localhost absolute-path


    【解决方案1】:

    您希望在 www.example.com 的文档根目录中包含这样的内容:

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

    【讨论】:

    • 我拥有 example.com 并且可以编辑 htaccess,但这样做将不允许人们使用我的网站,因为请求将被重定向到 localhost 并且不会加载资源。我想这样做来测试我的网站,其中包含本地主机内的绝对路径。可以做点什么吗?我只想在不更改绝对路径的域名的情况下本地测试我的站点太离线,即我不想在源代码中将 example.com 更改为 127.0.0.1 来测试它。
    • @ShubhamBadal 不使用 htaccess,除非您明确访问 example.com 站点,否则 htaccess 文件不会执行任何操作。您可以在您的计算机上创建一个主机记录,将example.com 指向127.0.0.1
    • 是的,我猜你是 ryt,bcz html 向网站发送请求并且 localhost 无能为力,我尝试通过编写 127.0.0.1 www.example.com 将其添加到 sys32 中的主机,但后来我无法访问我的网站。我不希望整个网站被重定向。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 2011-03-02
    • 2017-11-27
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    相关资源
    最近更新 更多