Skip to content

How to redirect a URL with .htaccess

To redirect a URL with .htaccess, add one line to the .htaccess file in your site's root folder: Redirect 301 "/old-page" "https://example.com/new-page". Apache then sends everyone who asks for /old-page to the new address with a permanent 301. Without the 301, Apache sends a temporary 302.

Redirect checkerUpdated 4 min read

The examples below come from Apache's own documentation for mod_alias and mod_rewrite. Swap example.com for your domain.

Before you start

  • .htaccess or the main config? Apache's guide says that if you can edit the main server configuration, put redirects there: it's read once at start-up instead of on every request. .htaccess is for shared hosting and control panels where you can't.
  • The file is per folder. Rules in a folder's .htaccess apply to that folder and everything under it, so put site-wide redirects in the one at the top of your site.
  • Keep a copy. Apache answers with a server error (500) when .htaccess holds a line it doesn't accept, which takes the site down until it's fixed. Save the old file before you edit, so you can put it back.

Redirect one page

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

The first path is the old address, starting with a slash. The second is the full new address, or a path on the same site such as "/new-page". Apache matches whole path segments, so this line doesn't catch /old-page.html-2.

Redirect permanent means the same as Redirect 301. You can also write RedirectPermanent "/old" "/new".

Redirect a folder or a whole site

Redirect matches the start of the path and carries the rest over:

Redirect 301 "/blog" "https://blog.example.com"

A request for /blog/2026/spring.html goes to https://blog.example.com/2026/spring.html, and query strings come along too. To move a whole site to a new domain, redirect the root:

Redirect 301 "/" "https://www.newdomain.com/"

Redirect with a pattern

RedirectMatch takes a regular expression, for when a simple prefix isn't enough. This sends every GIF to a JPG of the same name on another server:

RedirectMatch 301 "(.*)\.gif$" "https://images.example.com$1.jpg"

Force https in .htaccess

Apache's recommended way to force https is a Redirect in the port 80 virtual host. If you can only use .htaccess, its documentation gives this mod_rewrite rule:

RewriteEngine On
RewriteCond "%{HTTPS}" !=on
RewriteRule "^(.*)" "https://%{SERVER_NAME}$1" [R=301,L]

R=301 makes the redirect permanent. If your host puts a load balancer in front of Apache, Apache's guide says %{HTTPS} will always report off, even for https visitors. Apache's guide suggests checking the X-Forwarded-Proto header instead in that case.

Check the redirect

Open the old address in a private window to make sure you land on the right page. Then paste it into the checker below. It shows each hop and its status code, and flags any redirect that isn't a permanent 301 or 308.

Redirect checker

Open the full tool

301, 302 and the rest

The number in the rule is the HTTP status code. RFC 9110, the HTTP standard, says a 301 means the page has a new permanent address and links ought to be updated. A 302 means the move is temporary and the old address stays in use. 307 and 308 are the temporary and permanent versions that also keep the request method, which matters for forms that post data. Use 301 for most moved pages.

You can also tell Apache a page is gone for good: Redirect gone "/old-offer" answers 410 with no new address.

When a redirect doesn't work

  • Nothing happens. Apache's guide says the usual cause is AllowOverride None, a server setting that makes Apache ignore .htaccess. Its test: type a nonsense word in the file and reload. No 500 error means the file is being ignored, so ask your host. Also check the file name starts with a dot.
  • A loop. Two rules send the address back and forth, or an https rule fights a rule in your CMS. The checker shows the loop hop by hop.
  • Several hops. An old rule points at an address that itself redirects. Point every old address straight at the final one: see redirect chains.
  • The old page still shows. Browsers may remember a 301. Test in a private window.

To redirect a whole domain without a server of your own, see how to redirect a domain.

Sources

Questions

How do I redirect URLs in Apache?

Add a Redirect line to your site's configuration or to the .htaccess file in its root folder: Redirect 301 "/old-page" "https://example.com/new-page". Apache's documentation prefers the main configuration when you can edit it.

How do I redirect HTTP to HTTPS in Apache?

Apache's preferred way is a Redirect permanent line in a separate virtual host for port 80. If you can only use .htaccess, the documented mod_rewrite rule checks %{HTTPS} and redirects with R=301.

Why does my .htaccess redirect send a 302?

Apache's Redirect directive is temporary (302) unless you give it a status. Write Redirect 301 or Redirect permanent, then check the address again.

What is a redirect URL example?

Redirect 301 "/menu" "https://example.com/food/menu" sends anyone who asks for /menu to the new address, and tells search engines the move is permanent.