• How To?
  • Tips ‘n Tricks
  • WordPress
  • Snippets
  • Software
    • Browsers
    • Downloads
  • Web
  • Tools
    • Character Counter
    • chmod Calculator
    • Entities Encoder
    • Live HTML Editor
    • My IP
  • Contact
Twitter Facebook Google+ RSS
You are here: SumTips » Snippets » htaccess Snippets » Ultimate htaccess Redirects List

Ultimate htaccess Redirects List

Posted on December 10, 2011 by Renji | Short URL: http://sumtips.com/?p=6042

A list of useful htaccess redirects that you can easily implement on your site.

Redirect removed pages
Redirect deleted pages back to your site’s home page, or anywhere you want.

RedirectMatch 301 /page1/?$ http://mydomain.com/
RedirectMatch 301 /page2/?$ http://mydomain.com/

If all pages share a common pattern, we can rewrite this in single-line format:

RedirectMatch 301 /(page1|page2)/?$ http://mydomain.com/

Redirecting removed pages back to your home page preserves page rank that they may have accumulated, but if you rather like search engines or any other resources liking to the pages to forget about it, use the next redirect.

Declare page dead
By using the following redirect, you are sending a 410 status. Which means that the resources is permanently gone.

RedirectMatch gone /page1
RedirectMatch gone /page2

As before, we can optimize the code by writing this in a single line:

RedirectMatch gone /(page1|page2)

Note that here we are not using /?$ characters. So this will match any URL that begins with /page1, such as:
/page1-1
/page1-is-awesome

Redirect a static page to another site

Redirect 301 /old/old.html http://www.mynewdomain.com/new.html

Redirect a dynamic page to another site
A dynamic page is one generated by a database driven application. Such pages have a file name appended by a query string, looking something like this:

For this, the 301 redirect used for static pages will not work. For the above example, you’ll need to add this in your htaccess file:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=61$
RewriteRule ^/page.php$ http://www.mynewdomain.com/new.html? [R=301,L]

Redirect a dynamic URL to a new single file
Redirect request to dynamic URL’s to a single file
static file:

RewriteRule ^page.php?id=(.*)$ /new.html [R=301,L]

Here, a request to a dynamic URL such as
will be redirected to

Redirect file names with spaces
If you have a file with a space in its name, for example “old page.html”, a standard redirect 301 line won’t work, To redirect such files, include the file name in double quotes.

Redirect 301 "/old page.html"  

Redirect an entire category to another site

RedirectMatch 301 ^/category/awesomestuffs/?(.*)$ http://mynewdomain.com/category/awesomestuffs/$1

Redirect an entire directory to another site

Redirect 301 /olddirectory http://mynewdomain.com/newdirectory/

Redirect an old domain to new domain

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.mynewdomain.com/$1 [R=301,L]

OR

Redirect 301 / http://www.mynewdomain.com/

Redirect all files in a folder to one File
Lets say you are no longer running ‘Christmas Giveaway’ and hence want to redirect all requests to the folder /awesomegiveaway to a single page called /end-of-giveaway.php. This redirect can be accomplished by the following:

RewriteRule ^awesomegiveaway(.*)$ /end-of-giveaway.php [R=301,L]

Redirect all files in a folder to one file, except one
In the above example, if you want one file to redirect to some other location, then that can be accomplished like this:

RewriteRule ^awesomegiveaway/special.html /special-offer.html [R=301,L]
RewriteRule ^awesomegiveaway(.*)$ /end-of-giveaway.php [R=301,L]

Once implemented, all files from /awesomegiveaway/ directory will redirect to the /end-of-giveaway.php file, except /awesomegiveaway/special.html which will redirect to /special-offer.html

Redirect visitors to a temporary Page
While you do some maintenance work on your site, you can redirect your visitors to a notice page and tell them the reason of downtime of your site.

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/ notice.html$
RewriteRule .* /maintenance.html [R=307,L]

Redirect to new file extension
This will redirect requests for any .html page to .php (ie is redirected to .

RedirectMatch 301 (.*)\.html$ http://www.mydomain.com$1.php

Redirect non-www to www domain
This will redirect all non-www requests to your site to the www version:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Redirect www to non-www domain
This does the opposite of the above. The following code wil redirect all www requests to your site to the non-www version.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC]
RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]

For more htacces hacks, check out this post.


Tweet

Related posts:

  • 35 .htaccess Hacks for Web Developers
  • Enable HTTPS / SSL Browsing on Website, Folder or a Page
  • Exclude Certain Folders from htaccess Rewrite Rules
  • Prevent Access to Files on Web Server using htaccess
  • Test Your htaccess Rules Online
Categories: htaccess Snippets
Exclude Certain Folders from htaccess Rewrite Rules

  • Get Updates via Email

  • Recent Posts

    • CMS2CMS: Migrate Site from Drupal or Joomla to WordPress
    • WordPress: Add Preview Button in Distraction Free Writing
    • How to Activate or Deactivate Individual Jetpack Modules
    • Windows 8: Auto Update Defender with Windows Update Disabled
    • Automatically Start and Close Programs at Specific Time
    • How to Copy Code from Notepad++ with Syntax Highlighting
  • Random Posts

    • Set Gravatar as Favicon in Your WordPress Blog
    • Change Windows Store Region to View Apps from Other Countries
    • Automatically Back Up Your Flash Drives and Memory Cards
    • Write Email in Markdown on Chrome, Firefox, and Thunderbird
    • Get Google Earth Functionality In Google Maps
    • Get Back Old Facebook Chat with Facebook Sidebar Chat Reversion
  • Categories

    • Blogging
    • Games
    • Google
    • How To?
    • Linux
    • Microsoft
      • Windows
    • Miscellaneous
    • Phone
    • Snippets
      • AutoHotkey Snippets
      • CSS Snippets
      • htaccess Snippets
      • JavaScript Snippets
      • PowerShell Snippets
      • WordPress Snippets
    • Social
    • Software
      • Browsers
    • Tips 'n Tricks
    • Wallpapers
    • Web
© SumTips. Contact | Sitemap | Privacy Policy