2015-11-16 69 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]