2015-11-16 40 views
0

我用这个改写动态目标网址htaccess的URL重写

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

域/主/ subold/whatever.php重定向到域/主/ subnew-MONTHNAME/whatever.php

我改变subnew-monthname文件夹的monthname部分每月。我可以在htaccess中每个月更改一次,所以带有subold的url将被重定向到带有subnew-monthname的url,但我想知道是否可以使“monthname”部分变为动态,因此它只是搜索月份名称或而不需要不断更改htaccess中的规则。

有什么建议吗?

回答

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); 
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]