【问题标题】:htaccess RewriteRule in CodeIgniterCodeIgniter 中的 htaccess 重写规则
【发布时间】:2013-05-18 06:12:05
【问题描述】:

我在 htaccess 中遇到了这个问题。我使用 CodeIgniter 并从链接中删除了“index.php”:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(robots\.txt|sitemap\.xml)

RewriteRule ^(.*)$ /index.php/$1 [L]

这很好用。

但是现在,我有一个旧网站,上面有这样的旧链接:

http://www.example.com/index.php/about_us/article/70,about-us

它们已被 Google 编入索引。我想将它们重定向到新的链接结构。

我需要更改 htaccess,所以上面的链接会重定向到如下所示的链接:

http://www.example.com/en/about_us/70,about-us

'index.php' 被删除,没关系。 “文章”并不总是显示在链接中,仅显示在某些链接中,但它始终是一个片段。 而且我必须在前面添加'en'(lang)。

我该怎么做?

【问题讨论】:

    标签: codeigniter .htaccess url-rewriting


    【解决方案1】:

    试试这些;

    .htaccess

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(robots\.txt|sitemap\.xml)
    RewriteRule ^en/(.*)$ /index.php/$1 [L]
    

    routes.php

    $route["about_us/(:any)"] = "about_us/article/$1";
    

    有关 url 路由的更多信息: URI Routing

    【讨论】:

    • 但在网络语言中只有在谷歌语言中不存在。所以我会得到双重语言,如:en/en/... 并且在溃败中我重写了 globaly 因为'about_us'只是许多生成的动态sgements之一。所以我只需要 htacces 并重写条件我猜
    • Noup。这不是那么简单。我只想重定向谷歌链接。网络中的链接还可以,它们可以像现在一样工作。
    【解决方案2】:

    这应该让你接近(未经测试,只是为你拼凑)。您可能需要调整第一条规则以检查 en 段是否存在,因为如果存在,您最终会在重定向的 URL 中出现双精度值。

    RewriteEngine On
    
    # Redirect any URI's starting with "index.php" to new URI
    RewriteRule ^index.php/(.*)$ http://www.example.com/en/$1 [R=301,L]
    
    # Route URI's to index.php front controller
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(robots\.txt|sitemap\.xml)
    RewriteRule ^(.*)$ index.php/$1 [QSA,L]
    

    【讨论】:

      猜你喜欢
      • 2015-02-15
      • 1970-01-01
      • 2023-03-25
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多