【问题标题】:Access old site after redirection重定向后访问旧站点
【发布时间】:2013-08-29 19:44:08
【问题描述】:

我需要从客户旧站点到新站点的永久重定向,重定向是从 hissite.eu 到 newsite.com,相同的托管但其他域。现在,他希望仍然可以通过某个子域访问他的旧站点,如果我可以让它工作,这不会有问题,但是旧站点是用自定义 CMS 制作的,很多硬编码,如果我移动它就不起作用到任何目录。

所以我的问题是,如果我从 oldsite.eu 永久重定向到 newsite.com,我怎样才能让他访问他的旧网站?如果他键入 ip 地址而不是域,是否有可能以某种方式不重定向,但我想再次管理面板不起作用。是否可以对除特定 ip 之外的所有人进行重定向?

用当前的 htaccess 更新:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

Rewriterule ^index$                             index.php [L]
Rewriterule ^login$                             login.php [L]
Rewriterule ^register$                          register.php [L]
Rewriterule ^classified_new$                        classified_new.php [L]
Rewriterule ^account_classifieds$                   account_classifieds.php [L]
Rewriterule ^account_classifieds_manage$                account_classifieds_manage.php [L]
Rewriterule ^account_confirm$                   account_confirm.php [L]
Rewriterule ^account_details$                   account_details.php [L]

Rewriterule ^account_invoice_browse$                account_invoice_browse.php [L]
Rewriterule ^account_invoice_details$               account_invoice_details.php [L]
Rewriterule ^account_main$                      account_main.php [L]
Rewriterule ^account_messages$                  account_messages.php [L]
Rewriterule ^account_profile$                   account_profile.php [L]
Rewriterule ^ajax_request$                      ajax_request.php [L]
Rewriterule ^classified_browse$                     classified_browse.php [L]
Rewriterule ^classified_details$                    classified_details.php [L]
Rewriterule ^classified_new_form$                   classified_new_form.php [L]
Rewriterule ^classified_new_premium$                classified_new_premium.php [L]
Rewriterule ^classified_payment$                    classified_payment.php [L]
Rewriterule ^classified_new_confirmation$           classified_new_confirmation.php [L]
Rewriterule ^classified_payment_process$                classified_payment_process.php [L]
Rewriterule ^classified_payment_response$           classified_payment_response.php [L]
Rewriterule ^classifieds_json$                  classifieds_json.php [L]
Rewriterule ^convert$                           convert.php [L]
Rewriterule ^help$                          help.php [L]
Rewriterule ^login_process$                         login_process.php [L]
Rewriterule ^search_advanced$                   search_advanced.php [L]
Rewriterule ^verification_image$                    verification_image.php [L]

#search result
Rewriterule ^search/(.*)$ search_results.php?q=$1 [L]

#classified detail
Rewriterule ^classified/(.*)$ classified_details.php?id=$1 [L]
</IfModule>

【问题讨论】:

  • 与旧站点是否在同一个IP地址?
  • 您如何将旧网站重定向到新网站?这是什么类型的托管(vps、共享...试图确定您是否有权访问实际的 Web 服务器配置)?
  • 它的IP地址不同。我想我可以访问网络服务器配置,他们正在使用 plesk,所以需要更多时间来研究,真的讨厌 plesk。

标签: php .htaccess redirect url-redirection server-administration


【解决方案1】:

可以重定向除给定 IP 之外的所有 IP,您可以在重定向中添加条件,例如:

RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx$
RewriteRule ^ http://newdomain.com%{REQUEST_URI} [R=301,L]

对于多个 IP:

RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx$
RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx$
RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx$
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ http://newdomain.com%{REQUEST_URI} [R=301,L]

或者如果 IP 具有相同的块 192.168.0.1、192.168.0.2、192.168.0.3:

RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ http://newdomain.com%{REQUEST_URI} [R=301,L]

xxx\.xxx\.xxx\.xxx 更改为您的客户端 IP。

但是这会给您带来一些问题,例如:

  1. 您的客户端可能已经缓存了重定向,因此您需要要求他临时使用其他浏览器或清除他的缓存(这并不总是有效)。

  2. 如果您删除了 .htaccess 上可能存在的旧规则,它将无法按预期工作。

  3. 如果您的客户有动态 IP,您需要在他每次尝试访问它时更改它。

可能还有更多,但这是最常见的。

【讨论】:

  • shift + reload 总是有效的。您也可以禁用自动重定向。
  • @hakre shift+reload 不是真的,它取决于一系列的东西,包括系统的本地化。
  • 好吧,如果您在浏览器中禁用自动重定向,您总是有机会切换+重新加载,因为 URL 尚未更改。
  • @hakre 还有其他方法,但是,我会尽量做到最简单的方法,不要让客户的生活过于复杂,比如让他们在他们的机器上放置任何类型的配置或必须除了简单地访问网站之外,还可以做任何事情。答案,我的和 anubhava 都会被缓存,但这只是暂时的,只是使用不同的浏览器会破坏它。
  • 其实你不需要 OR ;) 我的坏已经改变了它。
【解决方案2】:

是否可以对除特定 ip 之外的所有人进行重定向?

是的,这是完全可能的。我实际上建议你这样做,因为它最容易使用。或者,您也可以查找特定的请求标头,以便客户可以查看旧站点或新站点并对其进行控制。另一种方法是使用 cookie。

现在具体而言,这取决于您使用的具体网络服务器,但大多数网络服务器都能够处理此类配置。

例如如果您将 Apache HTTPD 与基于 mod_rewrite 的重定向一起使用,则可以相应地制定 RewriteCond。

【讨论】:

    【解决方案3】:

    实现此目的的一种简单方法是由您的客户端传递一个秘密查询参数:(您可以根据需要使其更复杂)

    # set a cookie only if a secret URL e.g. http://oldsite.eu?q=q1w2e3r4t5
    # is passed
    RewriteCond %{QUERY_STRING} (^|&)q=q1w2e3r4t5(&|$) [NC]
    RewriteRule ^ - [L,CO=mredir:0:secret]
    
    # if cookie isn't set then redirect to new website
    RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]
    RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.eu$ [NC]
    RewriteRule ^ http://newsite.com%{REQUEST_URI} [R=301,L]
    

    编辑

    查看了您的 .htaccess,我强烈建议您合并多个 Rewriterule 行。请参阅下面的示例:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    Rewriterule ^(index|login|register|help)$ $1.php [L,NC]
    

    【讨论】:

    • 这听起来很棒!如果他继续浏览该网站,他会继续留在该网站上还是最终会被重定向?
    • @GoranJakovljevic 他最终会被重定向,具体取决于您可能对文件具有的其他规则,例如,如果某个规则从 URL 中删除了该查询字符串。
    • 查看了您的 .htaccess,我强烈建议您使用管道 | 使用 OR 条件将多个 Rewriterule ^anything$ anything.php [L] 行合并为一个(请参阅我上面编辑中的示例)
    • 还要回答您的第一个问题,客户只有在使用该秘密查询字符串时才能获得旧网站,否则它将被重定向到新网站。 (我们也可以使用 .htaccess 中的这个秘密查询字符串设置一些 cookie)
    猜你喜欢
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    相关资源
    最近更新 更多