【问题标题】:.htaccess redirect url with certain parameters to homepage.htaccess 将带有某些参数的 url 重定向到主页
【发布时间】:2013-12-05 21:40:57
【问题描述】:

参数为:“route=product/search”和“path=0”。我已经尝试了很多东西,但我对 .htaccess 的了解有限。

我想重定向以下网址: www.example.com/index.php?route=product/search&path=0

到 www.example.com

无论我尝试什么,它似乎都不起作用。最好的解决方案是什么?

# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/ 

RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [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]

### below send all calls to www
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]


RewriteCond %{QUERY_STRING} (^|&)route=product/search(&|$) [NC]
RewriteCond %{QUERY_STRING} (^|&)path=0(&|$) [NC]
RewriteRule ^index.php$ / [R=301,NC]

【问题讨论】:

    标签: .htaccess url parameters rewrite


    【解决方案1】:

    将这些指令添加到您的主要.htaccess:

    RewriteEngine on
    
    RewriteCond %{QUERY_STRING} (^|&)route=product/search(&|$) [NC]
    RewriteCond %{QUERY_STRING} (^|&)path=0(&|$) [NC]
    RewriteRule ^index.php$ /? [R=301,NC]
    

    在第四个 RewriteRule 中还缺少一个 RewriteCond(这会阻止以 index.php 开头的 url 被重写),您的 .htaccess 将是:

    # SEO URL Settings
    RewriteEngine On
    # If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/ 
    
    RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
    RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
    RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
    RewriteCond %{REQUEST_URI} !^index.php
    RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
    
    ### below send all calls to www
    RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    
    
    RewriteCond %{QUERY_STRING} (^|&)route=product/search(&|$) [NC]
    RewriteCond %{QUERY_STRING} (^|&)path=0(&|$) [NC]
    RewriteRule ^index.php$ /? [R=301,NC]
    

    【讨论】:

    • 亲爱的 Amine,感谢您的回答。可悲的是,它不起作用。我用我的 htaccess 文件更新了问题。
    • 我忘了在 rwriterule 后面加上一个反斜杠,这会阻止查询字符串被附加,我已经更新了你的 .htaccess,试一试(记得在测试前清除浏览器缓存)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-01
    • 2018-05-12
    相关资源
    最近更新 更多