【问题标题】:Add A 301 Redirect to Codeigniter .htaccess将 301 重定向添加到 Codeigniter .htaccess
【发布时间】:2013-01-09 04:26:05
【问题描述】:

我一直在尝试为最近移动的页面设置重定向。该页面最初位于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/

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

【问题讨论】:

    标签: .htaccess codeigniter mod-rewrite redirect


    【解决方案1】:

    RewriteBase / 文件解决问题后,在.htaccess 的顶部添加RewriteRrule

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 这可行,但我觉得这可能应该在站点的.htaccess 或 apache 设置中完成,因为如果其他开发人员必须在您的站点上工作,这将是他们首先查看的位置.将它放在控制器中不会是我认为找到这种逻辑的地方。
      猜你喜欢
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 2011-05-01
      • 2011-01-03
      • 2012-03-10
      相关资源
      最近更新 更多