繁体   English   中英

使用.htaccess的多个URL重定向

[英]Multiple url redirect using .htaccess

我有一个网站,它使用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可以重定向网址,例如

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

要么

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

目前,我正在使用php random array做到这一点。

<?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代码那样完全随机,但是您可以使用TIME_SEC变量(代表当前时间的秒数)使用mod_rewrite实现类似的功能。 考虑以下代码:

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重定向,如下所示: http : //www.inmotionhosting.com/support/website/redirects/setting-up-a-301-永久重定向消息通过-htaccess的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM