将301重定向添加到Codeigniter.htaccess

将301重定向添加到Codeigniter.htaccess,.htaccess,codeigniter,mod-rewrite,redirect,.htaccess,Codeigniter,Mod Rewrite,Redirect,我一直在尝试为最近移动的页面设置重定向。该页面最初位于http://example.com/foo/,但此后已移至http://example.com/foo/bar/ 我在我的站点.htaccess文件中尝试了以下规则: RedirectMatch 301^/foo/$/foo/bar/ 但是要转到urlhttp://example.com/foo/导致重定向到urlhttp://example.com/foo/bar/?/foo/。当url正常工作并且我想重定向到加载的页面时,我很想去掉ur

我一直在尝试为最近移动的页面设置重定向。该页面最初位于
http://example.com/foo/
,但此后已移至
http://example.com/foo/bar/

我在我的站点
.htaccess
文件中尝试了以下规则:

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

但是要转到url
http://example.com/foo/
导致重定向到url
http://example.com/foo/bar/?/foo/
。当url正常工作并且我想重定向到加载的页面时,我很想去掉url末尾额外的
?/foo/

这是我的完整访问权限:

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/

RewriteBase/
文件之后,将
RewriteRrule
添加到
.htaccess
的顶部解决了问题


RewriteRule^foo/$/foo/bar[R=301,L]
RewriteBase/
文件解决问题后,在
顶部添加一个
RewriteRrule


重写规则^foo/$/foo/bar[R=301,L]

我发现从控制器重定向比从.htaccess重定向更容易,因为.htaccess在末尾添加了一个querystring

例如,我在控制器的操作中添加了以下内容:

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

我发现从控制器重定向比从.htaccess重定向更容易,因为.htaccess在末尾添加了一个querystring

例如,我在控制器的操作中添加了以下内容:

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

这会起作用,但我觉得这可能应该在该站点的
.htaccess
或apache设置中完成,因为如果其他开发人员必须在您的站点上工作,这将是他们首先考虑的地方。把它放在控制器中并不是我想找到这种逻辑的地方。这是可行的,但我觉得这可能应该在站点的
.htaccess
或apache设置中完成,因为如果其他开发人员必须在您的站点上工作,这将是他们首先考虑的地方。把它放在控制器里,我想找不到这种逻辑。