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

时间:2012-02-07 17:40:41

标签: php .htaccess redirect

因此,如果访问者位于测试网站http://testing.site.com/app/article/123qwag,我该如何将他重定向到相同的文章,但在主应用的域中:http://www.app.com/article/123qwag

除了我,我将通过几个ips(动态的)来表明自己。哪个是实现此目的的最佳方式, php .httacces

有什么建议吗?

修改

注意:我正在使用 codeigniter框架

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

EDIT2 好的,这是我的一些.htaccess(结合:https://github.com/h5bp/html5-boilerplate/blob/master/.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>

3 个答案:

答案 0 :(得分:3)

您的测试子域.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/9HTTP 301中的所有内容重定向到{ {1}}并附加文件夹。

答案 1 :(得分:2)

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

<强> 1。 mod_rewrite解决方案

您可以在.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重定向?因为否则谷歌可能会将您的测试网站放入索引中。有关详情,请参阅此处:Google and the 301 redirects。这也会检查您的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']));
}

答案 2 :(得分:0)

在.htaccess uner DOCUMENT_ROOT

中使用此代码
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]
相关问题