我想使用.htaccess
文件配置Apache,以便将外部网页中的任何请求重定向到splash.html
,但是不应重定向内部请求:例如:
如果我在www.test.com
上,并且单击了导致www.mysite.com
的链接,则应重定向到www.mysite.com/splash.html
,但是如果我从wwww.mysite/com/products
请求www.mysite.com
,则应该将我带到www.mysite.com/index.php
您可以写出.htaccess,例如:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://([^.]+.)*mysite.com [NC]
RewriteRule ^(.*)?$ /splash.html [R,NC]
请记住,您可能来自网站下的任何子域,但是如果您只想将其限制在一个域中:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !www.mysite.com [NC]
RewriteRule ^(.*)?$ /splash.html [R,NC]
您可以在这里测试
update1:
注意:我假设您正在使用Linux,如果您使用的是Windows,请先查看我的评论。
我建议您通过以下内容隔离您的Local主机项目:
1-将您的项目放在特定路径中,假设在/var/www/test/,该路径包含.htaccess
2-创建apache虚拟主机,通过创建/etc/apache/sites-enabled/mysite.conf,包含以下内容:
<VirtualHost *:80>
ServerName mysite.com
ServerAlias www.mysite.com
DocumentRoot /var/www/test
<Directory /var/www/test>
DirectoryIndex index.php
AllowOverride All
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
3-将此行添加到您的/etc/主机的末端:
127.0.0.1 www.mysite.com mysite.com
4-重新启动apache
根据上述内容,您可以使用此URL(s)上的本地代码运行:
mysite.com
www.mysite.com
并进行您想要在本地进行的任何测试,而不会影响您在
下运行的其他项目 localhost
我通过创建简单的html页面http://localhost/test.html测试了上述逻辑
注意:如果您使用的是Windows,则可以执行以下替换:
/var/www/ => Apache root of your local server
/etc/hosts => c:WindowsSystem32Driversetchosts
/etc/apache/sites-enabled => virtual hosts of your local server
update2:
,如果您不喜欢与上面的虚拟主机一起使用
1-在apache root上创建文件夹,让我们命名为" mysite"
2-内部的序列上的.htaccess
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} ^http(s)?://localhost/mysite/ [NC]
RewriteRule ^(.*)?$ /splash.html [R,NC]
3-您可以使用test.html在" myite"文件夹之外测试重定向,并使用此代码
<a href="http://localhost/mysite/test.html">test</a>
4-您可以使用test.html在" mysite"文件夹中测试内部页面,并使用此代码
<a href="http://localhost/mysite/test.html">test</a>
您需要的是mod_rewrite条件匹配HTTP_REFERER
的值。这样的东西(从我的头顶 - 可能需要进行一些调整):
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !www.mysite.com [NC]
RewriteRule ^/?(.html)?$ /splash.html [R,NC]
http://httpd.apache.org/docs/2.2/rewrite/access.html
在您的/.htaccess文件中尝试一下
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://www.mysite.com [NC]
RewriteRule ^.* /splash.html [R,NC,L]
RewriteRule ^products/?$ /index.php [R,L,NC]