【问题标题】:opencart rewrite .htaccessopencart 重写 .htaccess
【发布时间】:2012-11-28 11:37:30
【问题描述】:

网站使用 opencart cms,启用了 SEO url,所以 .htaccess 看起来:

 RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

一切都很完美,但我想添加从非 www 到 www 的 301 重定向,所以我添加了:

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]

它有效,但是当我尝试使用类别或产品重定向链接时,它会添加“index.php?route="

例子:

“www.example.com/cats”,如果我尝试不带“www”的“example.com/cats”,链接将看起来 www.example.com/index.php?route=猫

【问题讨论】:

    标签: .htaccess url-rewriting opencart


    【解决方案1】:

    您需要添加所有重定向之前任何路由规则(例如,您有将事物路由到index.php 的规则。因此您的 htaccess 文件需要如下所示:

    # redirect rules first
    RewriteCond %{HTTP_HOST} ^mysite.com$ [NC]
    RewriteRule ^(.*) http://www.mysite.com/$1 [R=301,L]
    
    # then routing rules
    RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
    RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
    RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
    

    【讨论】:

    • 谢谢你,伙计。我不敢相信我浪费了 2 个小时来修复代码,我所需要的只是将重定向放在路由规则之前。哈哈
    • @RaShe 是的,这里的关键概念是,尽管您有在当前迭代中停止重写的L 标志,但 mod_rewrite 将继续遍历所有规则,直到 URI 停止更改。因此,当您首先使用路由时,会应用路由并且 URI 会更改,但第二次会应用重定向使用已重写的 URI
    猜你喜欢
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2017-09-23
    • 2013-07-08
    相关资源
    最近更新 更多