Php 从测试站点重定向到生产站点

Php 从测试站点重定向到生产站点,php,.htaccess,redirect,Php,.htaccess,Redirect,因此,如果访客位于测试现场http://testing.site.com/app/article/123qwag我如何将他重定向到主应用程序域中的同一篇文章:http://www.app.com/article/123qwag 除了我,我将通过几个IP(动态IP)来识别我自己。实现这一点的最佳方法是,php还是.httacces 有什么建议吗 编辑: 注意:我正在使用codeigniter框架 它将我重定向到www.app.comindex.php EDIT2 好的,这是我的一些.htacces

因此,如果访客位于测试现场
http://testing.site.com/app/article/123qwag
我如何将他重定向到主应用程序域中的同一篇文章:
http://www.app.com/article/123qwag

除了我,我将通过几个IP(动态IP)来识别我自己。实现这一点的最佳方法是,php还是.httacces

有什么建议吗

编辑:

注意:我正在使用codeigniter框架

它将我重定向到
www.app.comindex.php

EDIT2 好的,这是我的一些
.htaccess
(与:)

这个
.htaccess
对我来说非常有用,感谢您对重定向的帮助

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On

RewriteCond %{HTTP_HOST} ^app.com$
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
#  RewriteCond %{HTTPS} on
#  RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}

RewriteCond %{REMOTE_ADDR} !=12.23.45.67
RewriteRule ^(.*)$ http://www.app.com/$1 [R=301,L]

# RewriteBase equivalent - Localhost
RewriteCond %{HTTP_HOST} !^localhost$
RewriteRule . - [E=REWRITEBASE:/codeigniter-app/]

# RewriteBase equivalent - Development
RewriteCond %{HTTP_HOST} ^testing.app.com$
RewriteRule . - [E=REWRITEBASE:/app/]

# RewriteBase equivalent - Development
RewriteCond %{HTTP_HOST} ^www.app.com$
RewriteRule . - [E=REWRITEBASE:/]

# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ %{ENV:REWRITEBASE}index.php?/$1 [L]

#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ %{ENV:REWRITEBASE}index.php?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{ENV:REWRITEBASE}index.php?/$1 [L]
#RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>

选项+FollowSymlinks
重新启动发动机
重写cond%{HTTP_HOST}^app.com$
重写cond%{HTTPS}=在…上
重写cond%{HTTP_HOST}^www\..+$[NC]
重写cond%{HTTP_HOST}(+)$[NC]
重写规则^(.*)$http://www.%1/$1[R=301,L]
#在上重写cond%{HTTPS}
#重写规则(*)http://%{http\u HOST}%{REQUEST\u URI}
重写cond%{REMOTE_ADDR}=12.23.45.67
重写规则^(.*)$http://www.app.com/$1[R=301,L]
#重写基等效-本地主机
重写cond%{HTTP_HOST}^本地主机$
重写规则[E=REWRITEBASE:/codeigniter应用程序/]
#基础等效-开发
重写cond%{HTTP_HOST}^testing.app.com$
重写规则[E=REWRITEBASE:/app/]
#基础等效-开发
重写cond%{HTTP_HOST}^www.app.com$
重写规则[E=REWRITEBASE://]
#删除尾部斜杠(防止SEO重复内容问题)
重写cond%{REQUEST_FILENAME}-D
重写规则^(+)/$$1[L,R=301]
#删除用户对系统文件夹的访问权限。
#此外,这将允许您创建System.php控制器,
#以前这是不可能的。
#如果已重命名系统文件夹,则可以替换“系统”。
重写COND%{REQUEST_URI}^系统*
重写规则^(.*)$%{ENV:REWRITEBASE}index.php?/$1[L]
#当应用程序文件夹不在系统文件夹中时
#此代码段阻止用户访问应用程序文件夹
#提交人:Fabdrol
#将“应用程序”重命名为应用程序文件夹名称。
重写cond%{REQUEST_URI}^应用程序*
重写规则^(.*)$%{ENV:REWRITEBASE}index.php?/$1[L]
#检查用户是否试图访问有效文件,
#例如图像或css文档,如果这不是真的,它会发送
#请求index.php
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$%{ENV:REWRITEBASE}index.php?/$1[L]
#重写规则^(.*)$/index.php?/$1[L]

您的测试子域的
.htaccess
中的这个应该可以工作:

RewriteEngine on
RewriteBase /app
RewriteCond %{REMOTE_ADDR} !=123\.45\.67\.[8-9]
RewriteRule (.*) http://www.app.com/$1 [R=301,L]

这将启用重写引擎,将其基本路径设置为
/app
-文件夹,当IP与
123.45.67.8/9
不匹配时,使用
HTTP 301
将其中的所有内容重定向到
www.app.com
并附加文件夹。

测试子域的
.htaccess
中的这一个应该可以工作:

RewriteEngine on
RewriteBase /app
RewriteCond %{REMOTE_ADDR} !=123\.45\.67\.[8-9]
RewriteRule (.*) http://www.app.com/$1 [R=301,L]

这将启用
RewriteEngine
,将其基本路径设置为
/app
-文件夹,当IP与
123.45.67.8/9
不匹配时,使用
HTTP 301
将其中的所有内容重定向到
www.app.com
并附加文件夹。

我将使用mod_rewrite,因为它直接在服务器中处理,这样更快

1。mod_重写解决方案

您可以在
.htaccess
中使用以下语句将用户重定向到www.app.com:

RewriteEngine On
RewriteBase /app
RewriteCond %{HTTP_HOST} ^testing.app.com$
RewriteCond %{REMOTE_ADDR} !=12.34.56.78
RewriteCond %{REMOTE_ADDR} !=12.34.56.79
RewriteRule ^(.*)$ http://wwww.app.com$1 [R=301,NC]
为什么我们在这里使用301重定向?因为否则谷歌可能会把你的测试站点放到索引中。有关详细信息,请参见此处:。这也会检查你的IP

请确保已加载mod_rewrite。在许多服务器上,默认情况下不会激活它

2。PHP解决方案

使用PHP是可能的。您需要确保通过这些语句传递每个语句,以便可以将其包含在主配置文件中。它会很慢,但可能更容易部署,具体取决于您的托管环境

<?php
if($_SERVER['REMOTE_ADDR'] != "12.34.56.78" && $_SERVER['REMOTE_ADDR'] != "12.34.56.79") { // Your IPs
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: http://www.app.com/".str_replace("/^\/app/","",$_SERVER['REQUEST_URI']));
}

我会使用mod_rewrite,因为它直接在服务器中处理,因此速度更快

1。mod_重写解决方案

您可以在
.htaccess
中使用以下语句将用户重定向到www.app.com:

RewriteEngine On
RewriteBase /app
RewriteCond %{HTTP_HOST} ^testing.app.com$
RewriteCond %{REMOTE_ADDR} !=12.34.56.78
RewriteCond %{REMOTE_ADDR} !=12.34.56.79
RewriteRule ^(.*)$ http://wwww.app.com$1 [R=301,NC]
为什么我们在这里使用301重定向?因为否则谷歌可能会把你的测试站点放到索引中。有关详细信息,请参见此处:。这也会检查你的IP

请确保已加载mod_rewrite。在许多服务器上,默认情况下不会激活它

2。PHP解决方案

使用PHP是可能的。您需要确保通过这些语句传递每个语句,以便可以将其包含在主配置文件中。它会很慢,但可能更容易部署,具体取决于您的托管环境

<?php
if($_SERVER['REMOTE_ADDR'] != "12.34.56.78" && $_SERVER['REMOTE_ADDR'] != "12.34.56.79") { // Your IPs
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: http://www.app.com/".str_replace("/^\/app/","",$_SERVER['REQUEST_URI']));
}

在.htaccess uner文档根目录中使用此代码

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^testing\.site\.com$ [NC]
RewriteRule ^(app)/(article/(.*))/?$ http://www.$1.com/$2 [NC,L,R=301]

在.htaccess uner文档根目录中使用此代码

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^testing\.site\.com$ [NC]
RewriteRule ^(app)/(article/(.*))/?$ http://www.$1.com/$2 [NC,L,R=301]

对所有人来说是的,但是异常是如何发生的?对所有人来说是的,但是异常是如何发生的?这不考虑测试子域中的/app文件夹这不考虑测试子域中的/app文件夹我假设123qwag就是一个例子。我假设123qwag就是一个例子。