2013-01-25 49 views
0

我一直在嘗試爲最近移動的頁面設置重定向。該網頁最初是在http://example.com/foo/,但後來轉移到http://example.com/foo/bar/添加301重定向到Codeigniter .htaccess

我嘗試以下規則在我的網站.htaccess文件:

RedirectMatch 301 ^/foo/$ /foo/bar/

但是要到URL http://example.com/foo/導致重定向到URL http://example.com/foo/bar/?/foo/。雖然網址的作品和我想重定向到加載頁面,我很想擺脫在URL的末尾多餘的?/foo/

這裏是我的全部的.htaccess:

RewriteEngine On 
RewriteBase/

# add trailing slash 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} !(.*)/$ 
RewriteRule ^(.*)$ $1/ [R=301,L] 

# allow access to certain directories in webroot 
RewriteCond $1 !^(index\.php|robots\.txt|css/|lib/|js/|images/|^(.*)/images) 

# gets rid of index.php 
RewriteRule ^(.*)$ index.php?/$1 [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# page redirects 
RedirectMatch 301 ^/foo/$ /foo/bar/ 

回答

4

添加.htaccessRewriteRrule頂端RewriteBase /文件後,解決了這個問題。

RewriteRule ^foo/$ /foo/bar [R=301,L]

0

我發現它更容易從控制器,而不是重定向的.htaccess的.htaccess因爲加入了在最後一個查詢字符串。

比如我已經把這個在我的控制器的動作:

if ($this->uri->segment(2)==='old_url') { 
    redirect(base_url() . $this->lang->lang() .'/new-url', 'location', 301); 
} 
+0

這會的工作,但我覺得這個可能應該在'.htaccess'或Apache設置的現場進行,因爲這將是其他開發人員的第一位看他們是否必須在您的網站上工作。把它放在控制器中並不是我想要找到這種邏輯的地方。 – Jeemusu