动态目标url htaccess url重写

时间:2015-11-16 12:31:03

标签: .htaccess url mod-rewrite redirect

我使用这个重写

RewriteRule ^main/subold/(.*)$ /main/subnew-monthname/$1 [R=301,NC,L]

将domain / main / subold / whatever.php重定向到domain / main / subnew-monthname / whatever.php

我每个月都会更改subnew-monthname文件夹的monthname部分。我当然可以在htaccess每个月更改它,所以带有subold的url将重定向到带有subnew-monthname的url,但我想知道是否有可能使“monthname”部分动态,所以它只搜索月份名称或者月号不会不断改变htaccess中的规则。

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

main/subold/中,使用以下.htaccess规则创建mod_rewrite文件:

RewriteEngine on

RewriteCond %{REQUEST_URI} !(rewrites)
RewriteRule (.*)$ http://%{HTTP_HOST}/main/subold/rewrites.php?page=$1 [R=301]

然后,同样在main/subold/中,使用以下脚本创建rewrites.php文件:

$requestedURI = $_SERVER['REQUEST_URI'];
$whatever = str_replace('/main/subold/rewrites.php?page=','',$requestedURI);
$currentMonth = strtolower(date("F"));
$redirectedURI = '/main/subnew-'.$currentMonth.'/'.$whatever;

header("HTTP/1.1 301 Moved Permanently");
header('Location: '.$redirectedURI);

答案 1 :(得分:0)

我的其他答案有效(并且可能适用于其他情况),但我刚刚发现了 RewriteCond时间和日期变量,所以下面的答案可能正是你所做的那样。寻找:

main/subold/中,使用以下.htaccess规则创建mod_rewrite文件:

RewriteEngine on

RewriteCond %{TIME_MON} 01
RewriteRule (.*)$ http://%{HTTP_HOST}/main/subnew-january/$1 [R=301,NC,L]

RewriteCond %{TIME_MON} 02
RewriteRule (.*)$ http://%{HTTP_HOST}/main/subnew-february/$1 [R=301,NC,L]

RewriteCond %{TIME_MON} 03
RewriteRule (.*)$ http://%{HTTP_HOST}/main/subnew-march/$1 [R=301,NC,L]

RewriteCond %{TIME_MON} 04
RewriteRule (.*)$ http://%{HTTP_HOST}/main/subnew-april/$1 [R=301,NC,L]

RewriteCond %{TIME_MON} 05
RewriteRule (.*)$ http://%{HTTP_HOST}/main/subnew-may/$1 [R=301,NC,L]

RewriteCond %{TIME_MON} 06
RewriteRule (.*)$ http://%{HTTP_HOST}/main/subnew-june/$1 [R=301,NC,L]

RewriteCond %{TIME_MON} 07
RewriteRule (.*)$ http://%{HTTP_HOST}/main/subnew-july/$1 [R=301,NC,L]

RewriteCond %{TIME_MON} 08
RewriteRule (.*)$ http://%{HTTP_HOST}/main/subnew-august/$1 [R=301,NC,L]

RewriteCond %{TIME_MON} 09
RewriteRule (.*)$ http://%{HTTP_HOST}/main/subnew-september/$1 [R=301,NC,L]

RewriteCond %{TIME_MON} 10
RewriteRule (.*)$ http://%{HTTP_HOST}/main/subnew-october/$1 [R=301,NC,L]

RewriteCond %{TIME_MON} 11
RewriteRule (.*)$ http://%{HTTP_HOST}/main/subnew-november/$1 [R=301,NC,L]

RewriteCond %{TIME_MON} 12
RewriteRule (.*)$ http://%{HTTP_HOST}/main/subnew-december/$1 [R=301,NC,L]