重定向后访问旧站点

时间:2013-08-29 19:44:08

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

我需要将客户旧网站的重定向重定向到新网站,重定向是从hissite.eu到newsite.com,同样托管但其他域名。现在,他希望仍然可以通过一些子域访问他的旧网站,如果我可以使它工作,这将不会有问题,但旧网站是用自定义CMS,大量的硬编码很糟糕,如果我移动它只是不起作用到任何目录。

所以我的问题是,如果我将oldsite.eu的permament重定向转移到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>

3 个答案:

答案 0 :(得分:2)

可以重定向除给定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,则每次尝试访问时都需要更改它。

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

答案 1 :(得分:1)

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

# 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]

答案 2 :(得分:1)

  

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

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

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

E.g。如果您使用Apache HTTPD和基于mod_rewrite的重定向,则可以相应地制定RewriteCond。