.htaccess 301 redirect path and all child-paths

后端 未结 2 1849
温柔的废话
温柔的废话 2020-12-18 15:02

I want accesses to e.g. www.thisdomain.com/docs/path1/path2 to redirect to www.thatdomain.com/path1/path2

(Note that docs is not a part of the new path)

I ha

相关标签:
2条回答
  • 2020-12-18 15:34

    According to section "Redirect Old domain to New domain using htaccess redirect" of the first Google result which I found searching for "htaccess redirect" (without the double quotes), this piece of code will suffice:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
    

    According to their description of the technique, it will redirect the directory the .htaccess file is placed in recursively (including any child paths), just as you intend. Of course, mod_rewrite needs to be present for the rewrite directives to work.

    0 讨论(0)
  • 2020-12-18 15:36

    With regular expressions, * means any number of the previous atom, which will match /docs and /docs/. Try this:

    RewriteEngine on
    RewriteRule ^docs$ http://www.domain.com/ [R=301,L,QSA]
    RewriteRule ^docs/(.*) http://www.domain.com/$1 [R=301,L,QSA]
    

    (QSA is query string append, so /docs/foo?bar=baz won't lose the ?bar=baz.)

    0 讨论(0)
提交回复
热议问题