簡體   English   中英

HTACCESS REWRITE:忽略語言代碼並將index.php添加到url而不更改它

[英]HTACCESS REWRITE: Ignore Language code and add index.php to the url without changing it

我使用以下規則將帶有語言代碼(en,fr等)的URL重定向到沒有它的url,但在任何段之前添加了index.php。 但它沒有按預期工作。

我需要index.php,因為我使用的是codeigniter,一個php框架。

網址用戶訪問:

http://www.example.com/en/home

他們實際去的地方:

http://www.example.com/index.php/home

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(en|fr)/(.*)$ index.php/$2 [NC,L]  

如果我使用以下規則,重定向將起作用,但我不想更改URL。

RewriteRule ^(en|fr)/(.*)$ http://www.example.com/index.php/index.php/$2 [NC,L] 

您在網址中不需要index.php。 但為此,它取決於您的配置服務器。

在大多數情況下,我的團隊和我使用以下htaccess,我們編輯配置文件(config.php)將index_page設置為'',因為你的htaccess知道完美的東西。 但是,對於路由,讓codeigniter為您做這件事。

#.htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|system)
RewriteRule ^(.*)$ index.php/$1 [L]

並為您的application / config / config.php文件

/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = ''; // at the place of : 'index.php';

最后,您應該設置一些基本路線:

// application/config/config.php file
$route['default_controller'] = 'Home';
// other routes here
$route['^(en|fr)/(.+)$'] = "$2";
$route['^(en|fr)$'] = $route['default_controller'];

請注意,您的路線位置很重要。 (最精確到更少)

我鼓勵您閱讀官方網址文檔官方正則表達式路由文檔 ,並查看這個i18n庫 (在codeigniter 2和3上工作沒有問題)

我希望它能幫助你和其他人理解路線。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM