【问题标题】:In .htaccess remove word from URL在 .htaccess 中从 URL 中删除单词
【发布时间】:2012-06-06 00:45:48
【问题描述】:

经过一个多小时的搜索,我仍然无法弄清楚如何将链接从http://site/fr/other 重定向到http://site/other

我正在使用此代码:

RewriteEngine On
RewriteRule ^/fr/(.*)$ /$1 [L,R=301,QSA]

【问题讨论】:

    标签: regex .htaccess url mod-rewrite redirect


    【解决方案1】:

    只需删除第一个正斜杠:RewriteRule ^fr/(.*)$ /$1 [L,R=301,QSA]

    http://htaccess.madewithlove.be/ 试试这个。

    【讨论】:

    • 谢谢,你是对的,但是现在,如果用户键入 site/fr,它会给他们一个 404,但是 site/fr/ 可以工作。我应该为 case site/fr 添加规则吗?
    • 是的,您可以添加另一个规则:RewriteRule ^fr$ / [L,R=301,QSA]
    • 再次感谢,它成功了!组合起来看起来更干净。祝你有美好的一天
    • 哎呀,对不起。我删除了合并的一个 (RewriteRule ^fr((?:/.*$)?) $1 [L,R=301,QSA]),因为 http://site/france 会重定向到 http://site/ance,我认为这不是预期的行为。
    • 好吧,这个组合规则可能会更好:RewriteRule ^fr((?:(?:\s*|/.*)$)) $1 [L,R=301,QSA]http://site/france 不会重定向到 http://site/ancehttp://site/fr 重定向到 http://site/http://site/fr/ 重定向到 http://site/http://site/fr/other 重定向到 http://site/other
    【解决方案2】:

    从 url 中删除 '-xyz-'

    RewriteRule ^(.*)-xyz-(.*).html$ http://%{SERVER_NAME}/$1-$2.html [NC,R=301,L]
    

    从 url 'http://yoursite.com/xyz/some-url.html' 中删除 'xyz'

    RewriteRule ^xyz/(.*)\.html$ $1-watches.html [L,R=301]
    

    从 url 'http://yoursite.com/some-dir/xyz/some-url.html' 中删除 'xyz'

    RewriteRule ^(.*)/xyz/(.*).html$ http://%{SERVER_NAME}/$1/$2.html [NC,R=301,L]
    

    【讨论】:

      猜你喜欢
      • 2017-04-06
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 2011-05-20
      • 2017-03-15
      • 1970-01-01
      相关资源
      最近更新 更多