首页 > 解决方案 > 在 htaccess 文件中正确使用重定向

问题描述

我需要在新网站 ( https://myweb/new )的相应页面中重定向来自旧网站 ( https://myweb/ ) 的访问。所以我改变了我的https://myweb/.htaccess文件:

Redirect 301 /contact https://myweb/new/contact-us
Redirect 301 /blog https://myweb/new/my-blog
....

我认为重定向的工作方式类似于 if then exit:

if page is "/contact" then open https://myweb/new/contact-us and exit

在这些行之后,我编写了一些代码来重定向“else”语句中的请求:

RewriteRule (.*) https://myweb/new/ [R=301,L]

这是正确的解决方案吗?

标签: apache.htaccessredirect

解决方案


有这种方式:

RewriteEngine On

RewriteRule ^contact(.*)$ /new/contact-us$1 [L,NC,R=301]

RewriteRule ^blog(.*)$ /new/my-blog$1 [L,NC,R=301]

RewriteRule !^new/ /new%{REQUEST_URI} [L,NC,R=301]

确保在清除浏览器缓存后进行测试。


推荐阅读