Spring Sale! Save 30% on all books w/ code: PLANET24
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 = Designer. Developer. Producer. Writer. Editor. Etc.
Wizard’s SQL for WordPress: Over 300+ recipes! Check the Demo »

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 »
.htaccess made easy: Improve site performance and security.
Thoughts
I live right next door to the absolute loudest car in town. And the owner loves to drive it.
8G Firewall now out of beta testing, ready for use on production sites.
It's all about that ad revenue baby.
Note to self: encrypting 500 GB of data on my iMac takes around 8 hours.
Getting back into things after a bit of a break. Currently 7° F outside. Chillz.
2024 is going to make 2020 look like a vacation. Prepare accordingly.
First snow of the year :)
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.