Tools

htaccess Redirect Generator (301 and 302)

An .htaccess redirect is a rule in an Apache server configuration file that sends a visitor or search engine from one URL to another automatically. The generator below builds the exact redirect block for the most common cases, including a single page 301 or 302, forcing HTTPS, and adding or removing the www prefix, so you can copy it straight into your site root .htaccess file.

Short answer: an .htaccess redirect uses Apache directives like Redirect 301 or a RewriteRule to point an old address at a new one. Use a 301 when the move is permanent so search engines transfer ranking signals, and a 302 only when the change is temporary. Build your block above, paste it into the .htaccess file in your site root, and test before going live.
htaccess Redirect GeneratorBuild a ready-to-paste Apache .htaccess redirect block. Everything runs in your browser; nothing is sent anywhere.

What an .htaccess Redirect Is, and 301 vs 302

The .htaccess file is a configuration file the Apache web server reads on each request. A redirect rule inside it tells the server: when someone asks for this address, respond with a status code and a new location instead. The two codes you will use most are 301 and 302. A 301 means moved permanently, and it instructs browsers and search engines to treat the new URL as the real one. A 302 means found, a temporary move, so the original URL stays the canonical address. The difference matters because a 301 passes the old page’s accumulated ranking signals to the new page, while a 302 does not.

How to Use It

  • Choose the redirect type that matches what you are doing: a single page move, forcing HTTPS, or normalizing the www prefix.
  • For a single page redirect, enter the old path such as /old-page/ and the new path or full URL.
  • Copy the .htaccess block the tool generates.
  • Open the .htaccess file in your site root, or create one if it does not exist.
  • Paste the block, save the file, and load the old URL to confirm it lands on the new one.

The Redirect Types

TypeWhat it doesWhen to use it
Single page 301Permanently sends one old URL to a new URLA page moved or merged and you want its ranking to follow
Single page 302Temporarily points one URL at anotherA short-lived change, like a maintenance page or A/B test
Force HTTPSRedirects all http requests to httpsAfter installing an SSL certificate, to serve only secure URLs
Force wwwSends the bare host to the www versionTo make www.example.com your one canonical host
Remove wwwSends the www host to the bare versionTo make example.com your one canonical host
301 passes ranking signals, 302 does not. When a move is permanent, always use a 301 so the new URL inherits the old page’s authority. Reach for a 302 only when you genuinely intend to bring the original URL back, because search engines keep the original as the canonical address.

Where the File Lives and RewriteEngine On

The .htaccess file belongs in the root directory of your site, the same folder that holds your index file. Rules apply to that folder and everything below it. Any block that uses a RewriteRule, which includes the HTTPS and www rules, needs the line RewriteEngine On once before the rules to switch the rewrite module on. The simple Redirect 301 directive does not require it, but having RewriteEngine On present causes no harm. The file name starts with a dot, so it may be hidden in your file manager until you enable showing hidden files.

Common Mistakes and Redirect Loops

The error people hit most is the redirect loop, where a rule sends a URL back to itself or two rules point at each other. The force www and remove www rules above use a condition so they only fire on the wrong host, which prevents that loop. A second mistake is ordering: put the HTTPS and host rules near the top so they run before page-level redirects. A third is editing the wrong .htaccess, since WordPress and some plugins manage their own block between marker comments; add your rules outside those markers so they are not overwritten. Finally, a single typo in a path can take a site down, which is why you test on a copy first.

When to Use It

Use an .htaccess redirect whenever a URL changes and you want both people and search engines to follow it without seeing an error. Typical cases are renaming or merging pages, moving a site to HTTPS, settling on one canonical host, and cleaning up old links after a restructure. For a handful of redirects, editing .htaccess directly is the fastest route. For hundreds of mappings, a plugin or server rule set may be easier to manage, though the underlying directives are the same ones this tool produces.

Last Thoughts on Generating htaccess Redirects

A redirect is a small rule with large consequences. Get the status code right and the new URL keeps the trust the old one earned; get the order and the host conditions right and visitors never see a loop or a broken link. The generator above removes the guesswork by writing the exact Apache syntax for each common case, so the only step left is to paste, save, and test.

Build your redirect block, place it in your site root, and confirm the old URL resolves to the new one. To finish the canonical setup, generate a matching canonical tag and check your crawl rules with the robots.txt generator, and explore the rest of our free online tools.

Key Takeaways:

  • An .htaccess redirect is an Apache rule that sends one URL to another with a status code.
  • Use a 301 for permanent moves so ranking signals transfer; use a 302 only for temporary changes.
  • Force HTTPS and the www rules use a RewriteEngine On block with a condition that prevents redirect loops.
  • The file lives in your site root and applies to that folder and everything below it.
  • Order host and HTTPS rules near the top, and keep your rules outside any plugin-managed markers.
  • Always test a redirect on a staging copy before deploying, because one typo can break the site.

Frequently Asked Questions (FAQs)

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

A 301 is a permanent redirect that tells search engines the new URL is the real one and transfers the old page’s ranking signals to it. A 302 is a temporary redirect that keeps the original URL as the canonical address and does not pass those signals. Use a 301 for moves you do not plan to reverse, and a 302 only when the change is short-lived.

Where do I put the .htaccess redirect code?

Put it in the .htaccess file in the root directory of your site, the same folder as your index file. If no .htaccess file exists, create one with that exact name, including the leading dot. The rules apply to that folder and every folder beneath it.

Why do RewriteRule blocks need RewriteEngine On?

RewriteEngine On switches on the Apache rewrite module that interprets RewriteCond and RewriteRule lines. Without it, those rules are ignored. Include the line once before your rules. The simpler Redirect directive does not require it, but leaving it in place causes no problems.

How do I avoid a redirect loop?

A loop happens when a rule sends a URL back to itself. The force www and remove www rules above add a RewriteCond that only matches the wrong host, so the rule fires once and then stops. Keep host and HTTPS rules near the top of the file, and never redirect a URL to a destination that itself redirects back.

Will this work on Nginx or only Apache?

The .htaccess format is specific to Apache and Apache-compatible servers like LiteSpeed. Nginx does not read .htaccess files and uses a different syntax in its server configuration. If your host runs Nginx, you will need the equivalent rewrite or return rules in the server block instead.

Can I redirect a full URL instead of just a path?

Yes. For a single page redirect, the old value is a path relative to your domain root, but the new value can be either a path on the same site or a full URL on another domain. Enter the complete destination URL in the new field and the generator includes it in the block.

Nizam Ud Deen

Muhammad Nizam Ud Deen Usman is the founder of theCoreiTech and the author of The Local SEO Cosmos. Nizam works as an SEO consultant and content strategy expert with more than a decade of experience in digital marketing and IT, and he also founded ORM Digital Solutions, a digital agency serving medium and large businesses. He holds a degree from the University of Education, Lahore (Multan Campus), and was listed among the top 20 SEO experts in Pakistan in 2024. Nizam started theCoreiTech in 2012 to make computers easier to understand and use for everyone. Connect with Nizam on LinkedIn (seoobserver), X (@SEO_Observer), or at nizamuddeen.com.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button