2012-02-07 134 views
0

因此,如果訪問者位於測試現場http://testing.site.com/app/article/123qwag我如何可以重定向他在同一篇文章,但在主要應用的領域:http://www.app.com/article/123qwag重定向從測試現場生產現場

除了我,我將確定我自己通過幾個ips(動態的)。這是完成此操作的最佳方式,php.httacces

有什麼建議嗎?

編輯:

注:我使用笨框架

它重定向我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> 
+0

是的,但如何例外? – Alex 2012-02-07 17:46:28

回答

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可能會將您的測試網站放入索引中。詳情請看這裏: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'])); 
} 
+0

這不會照顧測試子域中的/ app-Folder – TimWolla 2012-02-07 17:47:13

+0

編輯:現在它確實。 – iblue 2012-02-07 17:48:48

+0

我相應地刪除了我的downvote – TimWolla 2012-02-07 17:49:42

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/9重定向那裏的所有內容HTTP 301www.app.com並追加文件夾。

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] 
+0

我假設123qwag就是一個例子。 – TimWolla 2012-02-07 17:47:58

+0

謝謝我編輯了我的答案。 – anubhava 2012-02-07 18:22:54