How to shorten a web address using .htaccess

Redirecting web addresses is a common use for .htaccess when addresses become too long or complicated. For example, if you had a blog running on your website and you wanted to share a post you had made the address could look something like:

http://www.mydomain.com/how-to-use-htaccess-to-redirect-long-urls/

Using .htaccess we could create a redirect that is easier to type and remember e.g.

http://www.mydomain.com/htaccess

This technique is often referred to as url rewriting. Here is what our .htaccess file would look like:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^/htaccess$ /how-to-use-htaccess-to-redirect-long-urls [L]
</IfModule>

Lines 1 and 4 - Wrap our rewrite rule in a container that checks rewriting is available.

Lines 2 - Turns rewriting on.

Lines 3 - The rule says if there is a request for 'www.mydomain.com/htaccess' rewrite (redirect) it to 'www.mydomain.com/how-to-use-htaccess-to-redirect-long-urls'. Finally the [L] means don't process any more rules.

Once you have created your .htaccess file you just need to upload it to the web folder for your hosting using FTP.

Check out some of our related guides

Need a hand? Search over a hundred step-by-step support guides