Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 如何在htaccess中获得if-else重定向?_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Apache 如何在htaccess中获得if-else重定向?

Apache 如何在htaccess中获得if-else重定向?,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,这是我在某个文件夹中的htaccess代码: <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^index\.php$ - [L] RewriteRule (.*) ./index.php?id=$1 [L] </IfModule> 重新启动发动机 重写规则^index\.php$-[L] 重写规则(.*)。/index.php?id=$1[L] 如果HTTP_REFERER是我自己的站点,但如果不是,则重

这是我在某个文件夹中的htaccess代码:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule (.*) ./index.php?id=$1 [L]
</IfModule>

重新启动发动机
重写规则^index\.php$-[L]
重写规则(.*)。/index.php?id=$1[L]
如果HTTP_REFERER是我自己的站点,但如果不是,则重定向到某个文件,我如何才能做到这一点

我一直在尝试类似的方法,但不起作用:

  <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_REFERER} ^http://.*MYSITE.*$ [NC]
    RewriteRule ^index\.php$ - [L]
    RewriteRule (.*) ./index.php?id=$1 [L]
    RewriteCond %{HTTP_REFERER} !^http://.*MYSITE.*$ [NC]
    RedirectMatch 301 ^(.*)$ MYFILE.jpg
</IfModule>

重新启动发动机
RewriteCond%{HTTP_REFERER}^HTTP://.*MYSITE.*$[NC]
重写规则^index\.php$-[L]
重写规则(.*)。/index.php?id=$1[L]
重写cond%{HTTP_REFERER}^http://.*MYSITE.$[NC]
重定向匹配301^(.*)$MYFILE.jpg
关于这个话题,我已经看过好几篇文章了,但都没有达到我想要的效果。谢谢

您可以使用:

 RewriteEngine on

#if http_referer is not my site then redirect the request 
RewriteCond %{HTTP_REFERER} !mysite\.com [NC]
RewriteCond %{REQUEST_URI} !/thispage\.php [NC]
RewriteRule (.*) http://example.com/thispage.php [L,R=301]
#rules for mysite.com
RewriteRule ^index\.php$ - [L]
RewriteRule (.*) ./index.php?id=$1 [L]

非常感谢。问题是,当它是一个内部页面时,它会进入一个无限的重定向漏洞。有什么想法吗?@llMagnus我添加了一个条件来防止循环错误。请将
/thispage.php
替换为非httpReferer URL将指向的文件,并清除浏览器缓存以测试这一点。你太棒了!非常感谢,这很好用。