.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




from Perishable Press https://ift.tt/2YaDds6

Comments

Popular posts from this blog

20 Free Professional Resume Cover Letter Format Templates for Jobs 2020

How to Create Gantt Charts in PowerPoint With PPT Templates

32+ Professional PowerPoint Templates: For Better Business PPT Presentations