2010-01-20 35 views
0

我必須每個URL所有_(下劃線)與被替換 - (連字符)現代重寫:更換所有下劃線(_)與連字符( - )

我目前做這種方式,但我尋找一個更簡單的方法來做到這一點,所以我不必每次URL變長時添加一行。

RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)-([^.]+)-([^.]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6_$7_$8_$10 [L] 
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)-([^.]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6_$7_$8_$9 [L] 
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6_$7_$8 [L] 
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6_$7 [L] 
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6 [L] 
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5 [L] 
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4 [L] 
RewriteRule ^([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3 [L] 
RewriteRule ^([^_]+)-([^.]+)$ index.php?/$1_$2 [L] 

謝謝。

回答

3

sedhyphen.sh

#!/bin/sh 
sed -u 's/_/-/g' 

的httpd的conf:

RewriteMap sed-hyphen prg:sedhyphen.sh 
RewriteRule ^(.*)$ index.php?/${sed-hyphen:$1} [L] 

確保sedhyphen.sh設置可執行。

+0

你能解釋一下我在哪裏把代碼sedhyphen.sh和代碼#/ bin/sh的和sed -u的/ _/-/G'! – cointilt 2010-01-20 21:25:50

+0

sedhyphen.sh爲您創建一個單獨的shell腳本。它應該放在'$ PATH'的某處。 – 2010-01-20 21:27:01

+0

這個文件可以在服務器上託管,就像使用ftp的regualr文件一樣嗎?還是我需要使用ssh在服務器上安裝它自己? – cointilt 2010-01-20 21:33:01

0

mod_rewrite並不是一種很好的工具。你爲什麼不用PHP取代?

$_SERVER['QUERY_STRING'] = str_replace('_', '-', $_SERVER['QUERY_STRING']); 

編輯那麼你可以嘗試這些規則集:

# using the path 
RewriteRule ^([^-]+)-([^-]+-.+) /$1_$2 [N] 
RewriteRule ^([^-]+)-(.+) index.php?/$1_$2 [L] 

# using the query 
RewriteRule ^([^-]+)-(.+) index.php?/$1_$2 
RewriteCond %{QUERY_STRING} ^([^-]+)-(.+) 
RewriteRule ^index\.php$ ?$1_$2 [N] 
RewriteRule ^index\.php$ - [L] 

但要小心的ñ標誌,你可以很容易地得到一個無限遞歸。

+0

我正在使用Codeigniter,因此網址必須在其中引用控制器功能。函數new_page(){}不能是new-page(){}函數。因此,這些網址只需要在瀏覽器網址中顯示不同的內容。 – cointilt 2010-01-20 21:23:41

+0

這與我上面的一樣,因爲你只有$ 1和$ 2。我有可能有超過10個_需要被替換 - – cointilt 2010-01-20 22:22:50

+0

@Will Ayers:只要應用規則,* N *標誌將負責處理並循環。但正如我所說的,mod_rewrite並不是最好的工具,因爲使用* N *是危險的。 – Gumbo 2010-01-20 23:22:59

0

也許這:

RewriteCond %{REQUEST_URI} ^(.*)_(.*)/$ 
RewriteRule (.*)_(.*)/ http://your-site.com$1-$2/ [R=301] 
+1

雖然這會工作,會導致每下劃線一個額外的HTTP交換。 – 2010-01-20 21:28:18

1

我知道這是老問題,但這裏是我的解決方案。它不浪費資源,它的搜索引擎優化友好,它不需要你使用bash,而是依靠php

將此放在您的.htaccess後重寫引擎已經啓動

RewriteCond %{REQUEST_URI} (.*)_(.*) 
RewriteRule (.*)$ /tools/underscore_to_hyphen.php?rewrite_uri=$1 [NC,L] 

這將發送所有文件php腳本underscore_to_hyphen.php中,你把這個代碼:

<?php 
$path = $_GET['unchanged_path']; 
$input_uri = $_GET['rewrite_uri']; 

$output_uri = str_replace("_", "-", $input_uri); 

//For redirect uncoment: 
header("HTTP/1.1 301 Moved Permanently"); 
header("Location: $path$output_uri"); 
exit(); 

//For rewrite uncoment: 
//include_once "$_SERVER[DOCUMENT_ROOT]$path$output_uri"; 
?> 

將發送它在正確的地方。

因爲這是.htaccess中沒有重定向的重寫規則,它將導致用戶/蜘蛛單個重定向或重寫。

請注意,這只是部分測試,我反向使用它,將-更改爲_

相關問題