htaccess 301 Redirect: Rules for Apache Websites

An htaccess 301 redirect is the simplest way to send old URLs to new ones when your site runs on Apache. Here is which rules you will need most often, how to order them, and how to avoid breaking the whole site with one stray line.

What .htaccess does and why it matters for your business

The .htaccess file is a small text file in the root folder of a site hosted on Apache. The server reads it on every request, so changes apply immediately with no restart. On shared hosting it is often the only way you can control URLs at server level. If your site runs on plain nginx, the file is ignored, so ask your hosting provider which server you actually have.

The business case is simple. You rebuilt your catalogue, renamed a section or moved to a new domain, but the old links still sit in your Instagram bio, a pinned Telegram post, OLX listings, printed price lists and partner websites. Without a redirect, customers land on a 404 page and search engines lose the authority the old address has built up. A proper 301 redirect carries both visitors and link value to the new page.

Prepare before you edit

The file is shared: WordPress and its plugins write their own rules into it too. A single typo usually produces a 500 error across the entire site, so work carefully.

  • Download a copy of the current file so you can restore it in a minute.
  • Confirm the server is Apache and that mod_rewrite is enabled.
  • Turn on hidden files in your file manager or FTP client, since the name starts with a dot.
  • Edit in a plain text editor, not a word processor.
  • Add rules one at a time and test the site after each.

Redirect a single page or a whole section

For individual URLs the Redirect directive is enough. The left side is the path from the site root, the right side is the full new address: Redirect 301 /catalog/sofa-lux/ https://example.uz/furniture/sofa-lux/

If you renamed a folder but the pages inside kept their names, one pattern rule covers the whole section: RewriteEngine On RewriteRule ^promotions/(.*)$ /offers/$1 [R=301,L] Anything under /promotions/ lands under /offers/ with the same ending. R=301 makes the redirect permanent and L stops further rules from running for that request. If you write plain R, the server sends a temporary 302, and search engines will not treat the move as final.

One canonical address: https and no www

A site is often reachable at four addresses at once: http or https, with or without www. To a search engine these are separate pages with identical content. A set of conditions merges them into one: RewriteEngine On RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} ^www\. [NC] RewriteRule ^(.*)$ https://example.uz/$1 [R=301,L] Read it as: if the connection is not secure or the host starts with www, send the visitor to the main domain over https.

One caveat. If the site sits behind a proxy or a service such as Cloudflare, Apache may not see the connection as secure, and the rule will loop; the browser then reports too many redirects. In that case check the condition against the header the proxy passes along, or enable the https redirect in your hosting panel instead.

Rule order and the mistakes that cause trouble

Apache reads rules from top to bottom. Put specific pages first, section rules next, and the https and canonical-host rule last. Place your own redirects above the CMS block: plugins can rewrite everything between the BEGIN WordPress and END WordPress comments, and lines you add inside it may vanish. Avoid mixing Redirect and RewriteRule without a reason, because they belong to different modules and run at different stages, which gives surprising results.

The usual suspects:

  • RewriteEngine On is missing before RewriteRule.
  • The R flag has no code, so you get a 302 instead of a 301.
  • Rules chain together: the old URL goes to an intermediate one, which goes to the final one.
  • The new URL also matches the old pattern, creating an endless loop.
  • Every deleted page is sent to the homepage, which search engines often treat as a soft 404.
  • Hundreds of individual Redirect lines slow down every request.

Testing the result and knowing when htaccess is not enough

After each change, open the site in an incognito window. Browsers cache permanent redirects, so a faulty 301 can seem stuck even after you fix it. Check the old URL, the new URL and a couple of pages the rule should not touch. Response codes and redirect chains show up in browser developer tools, with curl, or in an online header checker. If a 500 error appears, restore your saved copy and reread the last line you added.

When the list of URLs is very long, for example when an online shop with thousands of products moves, a CMS redirect plugin or the server configuration is a better home for the table. After a domain change, keep the old hosting paid until search engines have consolidated the sites and people stop arriving through saved links.

Redirects are only one piece of technical SEO. If you are planning a migration, a redesign or a clean-up of your URLs, the markdev team can audit the site and prepare a redirect map for it. Tell us about the task and we will suggest where to start.

FAQ

What is the difference between a 301 and a 302 redirect?

A 301 says the page has moved permanently, so search engines pass its value to the new address. A 302 is temporary. Use 301 for URL changes, domain moves and the switch to https.

Why does my site show a 500 error after editing .htaccess?

Usually a syntax error or a rule your host does not support, such as mod_rewrite being disabled. Restore the saved copy and add rules one by one.

Does .htaccess work on nginx?

No, nginx does not read it. Redirects there are set in the server configuration. If your site uses nginx in front of Apache, ask your host which files are honoured.

Can I redirect all deleted pages to the homepage?

It is better not to. Redirect each page to the closest relevant one. If there is no suitable replacement, a 404 or 410 response is the more honest answer.

Let’s look at your task for free

Leave your contact — in a 15-minute call we’ll show where leads get lost and what to fix first.