博客

由负责您托管服务的团队提供教程、指南、产品资讯和技术见解。

Performance and Technical SEO

301 Redirects with .htaccess: HTTPS, www and Old URLs

6 11月 2026 2 分钟阅读
分享此文章

When a page's address changes, visitors and search engines need to reach the new one. The standard way is a 301 (permanent) redirect. Our LiteSpeed server reads Apache-compatible .htaccess rules, so the examples below work as they are.

Before you start

  • The .htaccess file sits in your site's root folder (usually public_html). If you can't see it in File Manager, turn on hidden files under Settings.
  • Make a copy of the file before editing. A single typo can make the site return "500 Internal Server Error"; putting the old copy back fixes it.
  • On WordPress, put your own rules outside the # BEGIN WordPress block, preferably above it. WordPress rewrites the inside of that block itself.

Redirecting HTTP to HTTPS

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

If your site is behind Cloudflare with "Always Use HTTPS" turned on, you don't need this rule. Adding it while Cloudflare's SSL mode is "Flexible" causes a redirect loop; set the mode to "Full".

From www to non-www (or the reverse)

Having your site load at a single address stops the same content appearing at two.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]

For the reverse, always loading with www:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

On WordPress, the address in Settings › General must match the form you chose.

Redirecting a single old page

Redirect 301 /old-page/ https://example.com/new-page/

When you move a site or change its URL structure, redirect each old page that gets traffic to its closest new equivalent. Sending them all to the home page is a weak solution for users and search engines alike.

From an old domain to a new one

After adding the old domain to your account as an alias:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ https://newdomain.com%{REQUEST_URI} [L,R=301]

Don't block SSL validation

Broad rules that redirect every request can also block access to the free SSL certificate's validation file. If that happens, add this exception above the rule:

RewriteCond %{REQUEST_URI} !^/\.well-known/

See our AutoSSL troubleshooting article for details.

Testing

Browsers cache 301 redirects, so test changes in a private window. If you see a "too many redirects" error, two rules are triggering each other; remove the rule you added last and try again.

评论0

暂无评论

成为第一个分享想法的人。

参与讨论

请登录或注册后发表评论。

不错过任何文章

通过电子邮件接收我们最新的教程、指南和产品资讯。

您已订阅我们的电子通讯。