0

我一直在尝试为最近移动的页面设置重定向。该页面最初位于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 有效并且我想重定向到加载的页面,但我很想摆脱?/foo/url 末尾的额外内容。

这是我的完整 .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

2 回答 2

5

在文件RewriteRrule的顶部添加 a解决了问题。.htaccessRewriteBase /

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

于 2013-01-25T08:26:26.447 回答
0

我发现从控制器而不是 .htaccess 重定向更容易,因为 .htaccess 在末尾添加了一个查询字符串。

例如,我将其放入控制器的操作中:

if ($this->uri->segment(2)==='old_url') {
  redirect(base_url() . $this->lang->lang() .'/new-url', 'location', 301);
}
于 2015-06-07T12:46:22.940 回答