代码之家  ›  专栏  ›  技术社区  ›  Abilash Erikson

通过删除字符串进行URL重定向

  •  0
  • Abilash Erikson  · 技术社区  · 8 年前

    例如,如果url是

    www.example.com/shop   valid
    
    www.example.com/shop/red-product  need redirection to www.example.com/red-product
    
    www.example.com/shop/green  need redirection to www.example.com/green
    
    www.example.com/shop/any-string  need redirection to www.example.com/any-string
    

    我该怎么做。请帮忙。

    我当前的htacess文件是

        <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
        </IfModule>
    
    Redirect 301 /bio/ https://www.example.com/bio/
    
    Options -Indexes
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   anubhava    8 年前

    您可以用以下代码替换代码:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    
    RewriteRule ^shop/(.+)$ /$1 [L,NC,NE,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
    </IfModule>
    
        2
  •  1
  •   Abhishek Gurjar    8 年前

    下面的规则应该工作,我们是匹配组后,商店只。

    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/shop/(.*)
    RewriteRule ^ http://www.example.com/%1 [R]