Pro Plugin Sale! Save 25% on all pro plugins w/ code: SEASONS
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

Jeff Starr
About the Author
Jeff Starr = Designer. Developer. Producer. Writer. Editor. Etc.
BBQ Pro: The fastest firewall to protect your WordPress.

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 »
USP Pro: Unlimited front-end forms for user-submitted posts and more.
Thoughts
BF Sale! Save 40% on all Pro WordPress plugins and books w/ code FRIDAY23
Sincerely trying to engage more on social media. I love the people not the platforms.
All free and pro WordPress plugins updated and ready for WP version 6.4!
Fall season almost here :)
My greatest skill on social media is the ability to simply ignore 98% and keep scrolling without interacting.
Enjoying this summer, getting some great positive energy. Refreshing and inspiring.
☀️ Pro plugin giveaway! Enter to win 1 of 4 lifetime licenses for our WordPress security plugins, including 10-site Security Bundle!
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.