Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.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
Php 使用.htaccess进行多url重定向_Php_.htaccess_Mod Rewrite_Redirect_Url Redirection - Fatal编程技术网

Php 使用.htaccess进行多url重定向

Php 使用.htaccess进行多url重定向,php,.htaccess,mod-rewrite,redirect,url-redirection,Php,.htaccess,Mod Rewrite,Redirect,Url Redirection,我有一个网站,它是重定向到五个不同的索引文件随机使用php www.xyzdomain.com/index.php www.xyzdomain.com/index2.php www.xyzdomain.com/index3.php www.xyzdomain.com/index4.php www.xyzdomain.com/index5.php 我想使用.htaccess重定向它,就像重定向php一样 我发现使用htaccess我们可以重定向URL,比如 Redirect 301 / htt

我有一个网站,它是重定向到五个不同的索引文件随机使用php

  • www.xyzdomain.com/index.php
  • www.xyzdomain.com/index2.php
  • www.xyzdomain.com/index3.php
  • www.xyzdomain.com/index4.php
  • www.xyzdomain.com/index5.php
我想使用.htaccess重定向它,就像重定向php一样

我发现使用htaccess我们可以重定向URL,比如

Redirect 301 / http://www.xyzdomain.com/index.php

目前我正在使用php随机数组来实现这一点

<?php

$urls = array("index.php",
              "index2.php",
              "index3.php",
              "index4.php",
              "index5.php");
$url = $urls[array_rand($urls)];
header("Location: http://www.xyzdomain.com/$url");
?>


问题:如何使用.htaccess规则随机重定向多个索引?

不像PHP代码那样完全随机,但您可以使用表示当前时间秒值的
变量来实现类似的功能。考虑这个代码:

RewriteEngine On
RewriteBase /

# dont rewrite more than once
RewriteCond %{ENV:REDIRECT_STATUS} .+
RewriteRule ^ - [L]

# redirect all requests for php file to index file if %{TIME_SEC} is 0, 10, 20, 30, 40, 50
# or if %{TIME_SEC} is 1, 11, 21, 31, 41, 51
RewriteCond %{TIME_SEC} [01]$
RewriteRule ^.+?\.php$ index.php [L,NC]

# redirect all requests for php file to index file if %{TIME_SEC} is 2, 12, 22, 32, 42, 52
# or if %{TIME_SEC} is 3, 13, 23, 33, 43, 53
RewriteCond %{TIME_SEC} [23]$
RewriteRule ^.+?\.php$ index2.php [L,NC]

# redirect all requests for php file to index file if %{TIME_SEC} is 4, 14, 24, 34, 44, 54
# or if %{TIME_SEC} is 5, 15, 25, 35, 45, 55
RewriteCond %{TIME_SEC} [45]$
RewriteRule ^.+?\.php$ index3.php [L,NC]

# redirect all requests for php file to index file if %{TIME_SEC} is 6, 16, 26, 36, 46, 56
# or if %{TIME_SEC} is 7, 17, 27, 37, 47, 57
RewriteCond %{TIME_SEC} [67]$
RewriteRule ^.+?\.php$ index4.php [L,NC]

# redirect all requests for php file to index file if %{TIME_SEC} is ending in 8, 9
RewriteCond %{TIME_SEC} [89]$
RewriteRule ^.+?\.php$ index5.php [L,NC]

要将单个文件(如example.com/oldfile.htm)重定向到newfile.htm,可以使用301重定向,如下所示:

谢谢Anubhava。我在等你的回答!并参考您以前的答案!我会试试这个,你能简单解释一下这个方法吗?它是如何工作的?正如我前面提到的,
TIME\u SEC
变量表示当前时间的秒值。因此,基于秒数,上述规则将请求路由到不同的索引文件。如果时间是
14:33:00
14:33:01
,则它将路由到
/index.php
。如果时间是
14:33:02
14:33:03
,那么它将根据当前秒值路由到
/index1.php
,依此类推到其他索引文件。您可以使用
RewriteCond%{time_SEC}是的,也可以通过等分间隔来完成。谢谢Anubhava!它工作得很好!有一个问题!我认为.htaccess重定向方法比php方法快得多!不是吗?如果是,请简要说明!
RewriteEngine On
RewriteBase /

# dont rewrite more than once
RewriteCond %{ENV:REDIRECT_STATUS} .+
RewriteRule ^ - [L]

# redirect all requests for php file to index file if %{TIME_SEC} is 0, 10, 20, 30, 40, 50
# or if %{TIME_SEC} is 1, 11, 21, 31, 41, 51
RewriteCond %{TIME_SEC} [01]$
RewriteRule ^.+?\.php$ index.php [L,NC]

# redirect all requests for php file to index file if %{TIME_SEC} is 2, 12, 22, 32, 42, 52
# or if %{TIME_SEC} is 3, 13, 23, 33, 43, 53
RewriteCond %{TIME_SEC} [23]$
RewriteRule ^.+?\.php$ index2.php [L,NC]

# redirect all requests for php file to index file if %{TIME_SEC} is 4, 14, 24, 34, 44, 54
# or if %{TIME_SEC} is 5, 15, 25, 35, 45, 55
RewriteCond %{TIME_SEC} [45]$
RewriteRule ^.+?\.php$ index3.php [L,NC]

# redirect all requests for php file to index file if %{TIME_SEC} is 6, 16, 26, 36, 46, 56
# or if %{TIME_SEC} is 7, 17, 27, 37, 47, 57
RewriteCond %{TIME_SEC} [67]$
RewriteRule ^.+?\.php$ index4.php [L,NC]

# redirect all requests for php file to index file if %{TIME_SEC} is ending in 8, 9
RewriteCond %{TIME_SEC} [89]$
RewriteRule ^.+?\.php$ index5.php [L,NC]