Coding Cheatsheets - Learn web development code and tutorials for Software developers which will helps you in project. Get help on JavaScript, PHP, XML, and more.

Post Page Advertisement [Top]

This article is provided as a courtesy. Installing, configuring, and troubleshooting third-party applications is outside the scope of support provided by (mt) Media Temple. Please take a moment to review the Statement of Support.
Instructions

1. Create an empty text file using a text editor such as notepad, and save it as htaccess.txt.

NOTE:

The reason you should save the file as htaccess.txt is because many operating systems and FTP applications are unable to read or view .htaccess files by default. Once uploaded to the server you can rename the file to .htaccess.

2. Edit the contents of the file. Check the following examples:

301 (Permanent) Redirect: Point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "mt-example.com" domain:

# This allows you to redirect your entire website to any other domain
Redirect 301 / http://mt-example.com/

302 (Temporary) Redirect: Point an entire site to a different temporary URL. This is useful for SEO purposes when you have a temporary landing page and plan to switch back to your main landing page at a later date:

# This allows you to redirect your entire website to any other domain
Redirect 302 / http://mt-example.com/

 Redirect index.html to a specific subfolder:

# This allows you to redirect index.html to a specific subfolder
Redirect /index.html http://example.com/newdirectory/

 Redirect to a specific index page:

# Provide Specific Index Page (Set the default handler)
DirectoryIndex index.html

Part 1 - How do I redirect all links for www.example.com to example.com ?

Create a 301 redirect forcing all http requests to use either www.example.com or example.com:

    Example 1 - Redirect example.com to www.example.com:

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

    Example 2 - Redirect www.example.com to example.com:

            RewriteEngine on
            RewriteCond %{HTTP_HOST} ^www\.example\.com$
            RewriteRule ^/?$ "http\:\/\/example\.com\/" [R=301,L]

   Example 3 - Redirect https to http

           RewriteEngine On
           RewriteCond %{HTTPS} on
           RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

  Example 4 -Redirect http to https
       
          RewriteEngine on
          RewriteCond %{HTTPS} off
          RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

 Example 5 -Redirect from http://domain.com/index.php to http://www.domain.com/

         RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
         RewriteRule ^index\.php$ / [L,R=301]

Example 6 Create 404 Page

        ErrorDocument 404 /404.html

No comments:

Post a Comment

Bottom Ad [Post Page]