因此,如果访问者位于测试站点http://testing.site.com/app/article/123qwag
,我如何将他重定向到主应用程序的域中的同一篇文章:http://www.app.com/article/123qwag
除了我,我将通过几个ip(动态的)来识别自己。实现这一点的最佳方法是php还是.htmtacces?
有什么建议吗?
编辑:
注意:我使用codeigniter框架
它将我重定向到www.app.comindex.php
编辑2好的,这是我的一些.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>
测试子域的.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_rewrite解决方案
您可以在.htaccess
中使用以下语句将用户重定向到www.app.com/strong>:
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重定向?因为否则谷歌可能会把你的测试网站列入索引。请参阅此处了解详细信息:谷歌和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 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]