2012-08-04 65 views
0

我已經在codeigniter中開發了我的網站。我有一些事情要在.htaccess文件中完成。如何用「_」替換「 - 」並從地址欄中刪除index.php

  • 對於除index.phpimgjscss任何網頁/其他資源我希望它在URL中的實際資源之前添加index.php

  • 我有這麼多的靜態頁面unserscore「_」在其中,e.g,contact_us.phpour_services.php等。我想,當用戶給像www.mywebsite.com/our-services的URL應該打開原來的頁面,也就是www.mywebsite.com/our_services。有1,2高達7下劃線爲不同的頁面,例如,mywebsite.com/speech_writing_services

這裏是我的.htaccess文件,我可以建立至今:

RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/StyleAdmin|/images|/StyleAdmin/images|/css|/robots\.txt|/favicon\.ico) 
RewriteRule ^(.*)$ /index.php/$1 [L] 

RewriteRule ^([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2 [R=301,L] 
RewriteRule ^([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3 [R=301,L] 
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4 [R=301,L] 
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5 [R=301,L] 
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5_$6 [R=301,L] 

這裏的問題是,當我給它的網址像www.mywebsite.com/contact_us它工作得很好,並在地址欄中顯示相同的網址。但是,當我給這個網址www.mywebsite.com/contact-us它確實顯示的頁面,但在瀏覽器的地址欄中顯示的網址變成www.mywebsite.com/index.php/contact_us而我想它像www.mywebsite.com/contact_usindex.php刪除。

回答

0

您需要在重定向後執行路由。

RewriteEngine on 

RewriteRule ^([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2 [R=301,L] 
RewriteRule ^([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3 [R=301,L] 
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4 [R=301,L] 
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5 [R=301,L] 
RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)$ http://www.mywebsite.com/$1_$2_$3_$4_$5_$6 [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/StyleAdmin|/images|/StyleAdmin/images|/css|/robots\.txt|/favicon\.ico) 
RewriteRule ^(.*)$ /index.php/$1 [L]