Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
Web Dev + WordPress + Security

.htaccess Redirect Examples

Finally put together a giant list of .htaccess redirect examples. It’s meant as a quick copy-&-paste resource for those who may be looking for an assortment of redirect techniques. Here you will find redirects via mod_alias and mod_rewrite. Examples include redirecting to and from any directory, subdirectory, resource, URL, and much more. Most of these examples are taken from my previous article, Stupid htaccess Tricks; other examples are taken from previous .htaccess tutorials here at Perishable Press. Enjoy! :)

Redirect via mod_alias

These examples show how to redirect from one location to another using Apache’s alias module. Also, you can change the status code to whatever you want (for example, change from 301 to 302). And then learn how to go further with even more awesome .htaccess redirect examples.

Redirect a page on same domain

Redirect 301 /page.php /page.html

Redirect a page on different domain

Redirect 301 /page.php https://example.com/page.html

Redirect an entire site

Redirect 301 / https://example.com/

Redirect entire site to subdirectory

Redirect 301 / https://example.com/subdirectory/

Redirect from subdirectory to another site

Redirect 301 /subdirectory https://example.com/

Redirect from .html to .php

RedirectMatch 301 (.*)\.html https://example.com/$1.php

Forbid access to a page

Redirect 403 /page.html

Tell visitors a page is gone

Redirect 410 /page.html

Redirect via mod_rewrite

If you need more power, Apache’s rewrite module can do much more in terms of redirecting. Here are some examples:

Redirect from old page to new page

RewriteRule ^/old-page/?$ /new-page/ [R=301,L]

Redirect all paths from old domain to same paths on new domain

RewriteRule (.*) https://newdomain.com/$1 [R=301,L]

Redirect all paths from old domain to homepage of new domain

RewriteRule (.*) https://newdomain.com/ [R=301,L]

Redirect new domain with query string

RewriteRule (.*) https://www.newdomain.com/%{REQUEST_URI} [R=301,L]

Redirect new domain subdirectory with query string

RewriteCond %{REQUEST_URI} ^/subdirectory/(.*) [NC]
RewriteRule (.*) https://example.com/%1 [R=301,L]

Redirect URLs with ID query parameter

RewriteCond %{QUERY_STRING} ID=([0-9]+) [NC]
RewriteRule ^/index\.php /newpath/? [R=301,L]

Redirect URLs from subdirectory with ID query parameter

RewriteCond %{QUERY_STRING} ID=([0-9]+) [NC]
RewriteRule ^/subdirectory/index\.php /newpath/? [R=301,L]

Remove www from all URLs

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule (.*) https://example.com/$1 [R=301,L]

Require www for all URLs

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

Status Code

Note: You can change the status code (i.e., server response) for any of the previous redirects (either via mod_alias or mod_rewrite). Simply change the current 301 status to any of the following:

  • 301 — Permanent redirect
  • 302 — Temporary redirect
  • 403 — Forbidden
  • 410 — Gone
  • Any other valid status code

About the Author
Jeff Starr = Web Developer. Book Author. Secretly Important.
Blackhole Pro: Trap bad bots in a virtual black hole.

2 responses to “.htaccess Redirect Examples”

  1. you can modify the .htaccess to redirect http:// to https://? if you could teach us?

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
Wizard’s SQL for WordPress: Over 300+ recipes! Check the Demo »
Thoughts
I disabled AI in Google search results. It was making me lazy.
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources. Also: “The Web” is not a valid answer.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.