首页 文章

.htaccess将文件夹重定向到网址

提问于
浏览
26

我正在尝试将文件夹及其所有子文件重定向到带有.htaccess文件的URL .

Redirect 301 /abc/cba/ http://www.aaa.com/

/abc/cba/ddd/index.html 重定向到 http://www.aaa.com/ddd/index.html

我想要的是将 /abc/cba/ /abc/cba/ddd/index.html 重定向到 http://www.aaa.com/

有人可以帮忙吗?谢谢 . 如果有任何不清楚的地方,请告诉我 .

3 回答

  • 8

    默认情况下, Redirect 将路径节点映射到新路径节点,因此第一个路径之后的任何内容都会附加到目标URL .

    尝试:

    RedirectMatch 301 ^/abc/cba/ http://www.aaa.com/?
    

    或者,如果你更愿意使用mod_rewrite而不是mod_alias:

    RewriteEngine On
    RewriteRule ^/?abc/cba/ http://www.aaa.com/? [R=301,L]
    
  • 0

    这是另一个对我有用的mod_rewrite规则的例子

    我想将子目录重定向到同一域的根目录 .

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^sub_directory/(.*)$ /$1 [R=301,NC,L]
    </IfModule>
    

    更多例子可以在这里找到:http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/

  • 30

    我不得不将旧网站版本的网址重新路由到新版本,所以我这样做是为了重新路由从about-us / *到about-us.html的任何链接

    RewriteEngine on
    RewriteRule ^about-us/(.*)$ about-us.html [R=301,L]
    

    它没有做的是重写域名:domain.com/about-us/thing.html => domain.com/about-us.html .

    它适用于没有扩展名的东西domain.com/about-us/something-in-url => domain.com/about-us.html

    我添加了下面的行重定向.jpg和.png,但它不适用于.html,我找不到原因 .

    RewriteRule ^about-us/(.*).jpg about-us.html [R=301,L]
    RewriteRule ^about-us/(.*).png about-us.html [R=301,L]
    

相关问题