8-7 Mon-Sat   (509) 491 8580

Redirect & rewrite URL’s

Redirect & rewrite URL’s

Redirect and rewrite URL's

Redirect and rewrite URL’s is easy using an .htaccess file. .htaccess files enables you to redirect users from an old page to a new page.

Without having to keep the old page.

Redirect and rewrite URLsFor example, if you use index.html as your index file and then later rename index.html to home.html. You could set up a redirect to send users from index.html to home.html. For example:

Redirect & rewrite URL’s to a local site file.

Redirect /path/to/old/file/old.html /path/to/new/file/new.html

This is a redirect to an external site file.

Redirect /path/to/old/file/old.html http://www.example.com/new/file/new.html

The first path

IMPORTANT, The first path to the old file must be a local UNIX path, NOT the full path.

So, if the .htaccess file is in the directory /example.com. You would not include /home/exampleuser/example.com in the local UNIX path.

The first / represents the example.com directory. If the old file was in that directory, you would follow the / with the old file name.

The second path

The second path to the new file can be a local UNIX path. But can also be a full URL to link to a page on a different server or the same server.

Examples of redirect & rewrite URL’s

from an index.html file to a different directory

Redirect /index.html /new/

A redirect from index.html to default.html

Redirect /index.html /default.html

Redirect a local /private directory to another site’s private directory

Redirect /private/ http://www.example.com/private/

Redirecting error messages

You can also redirect 404 errors. Instead of throwing up a 404 page, this redirects to the homepage of the website.

ErrorDocument 404 http://example.com/

As well as redirect non-existing pages to index.php

If a visitor attempts to access a page that doesn’t exist, they are presented with a 404 error.

You can instead redirect the request to a non-existing page to your index.php file by adding the following code in your htaccess.

Options +SymLinksIfOwnerMatch 
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

If your index page isn’t index.php, just change the last line to your actual index file. Then the visitor is redirected back to your index page.

Forcing www in the URL

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

In the URL

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

Rewriting a URL

This example rewrites a URL to another URL. This rewrites example.com/1.html to example.com/abc.php?id=1

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9]+).html /abc.php?id=$1 [QSA,L]

The following explains the rules above:

allows any digit, and only any digit, 1 or more times.
allows any lowercase letter, plus “-” for word separation, 0 or more times. If you want it to support uppercase too, use “([a-zA-Z-]*). For example:
RewriteRule ^place/([a-zA-Z-]*).html /place/abc.php?id=$1 [QSA,L]
appends this to your internal scripting query string, and makes it the Last rewrite rule executed.

After using this method, you can retrieve the webpage with either address type.

This is handy for retrofitting a website that was not designed with mod_rewrite in mind. This is good because it does not destroy any bookmarks saved on users computers.

Rewriting non-existing links to index.php

The following redirects all links to files or folders that do not exist to index.php. However, if the file or directory does exist, it loads normally:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Forcing the domain to serve securely using HTTPS (any site)

The following forces any http request to be rewritten using https.

For example, the following code forces a request to http://example.com to load https://example.com.

It also forces directly linked resources (images, css, etc.) to use https:

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

If this isn’t working for you, first check your line endings.

Copy/paste from your web browser into a text editor may not work right. So after pasting into your text editor you should delete each line break and add it back in.

Get help with redirect & rewrite URL's

Contact us for help with redirect and rewrite URL’s

Related Posts