dongxue163536 2017-01-23 11:51
浏览 39
已采纳

.htaccess中这两个重定向之间的区别是什么,以及如何为这两个重定向编写单个重定向

I have new website done for one client, and now I am doing all redirects in .htaccess file. I was bit confused regarding the source URL part in lines below. Are these two lines works same way?

Redirect 301 /shop/contact-us http://www.example.com/contact-us/
Redirect 301 /shop/contact-us/ http://www.example.com/contact-us/
  • 写回答

2条回答 默认 最新

  • douyihuaimao733955 2017-01-23 13:25
    关注

    If you want to redirect /shop/contact-us with a number of optional trailing slashes to http://www.example.com/contact-us, then the Redirect directive is not very appropriate. Use RedirectMatch directive instead:

    RedirectMatch 301 "^/shop/(contact\-us)/?" http://www.example.com/$1/
    

    where

    • ^ is an anchor meaning "beginning of the line";
    • /? matches zero or one slash character;
    • (contact\-us) is a capturing group (referenced by $1)

    Note, the regular expression matches only the prefix, since only the ^ anchor is used. You can use the $ (end of the line) anchor in order to make the expression stricter, e.g.:

    RedirectMatch 301 "^/shop/(contact\-us)/*$" http://www.example.com/$1/
    

    where /* means zero or more slashes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?