Last Updated on April 17, 2021 by Amit
In this post you will learn how to shorten a long URL with very simple steps using RewriteRule directive in an htaccess file.
We are going to change a really long URL that is hard to type , hard to remember and not good for SEO in this article.
We have a URL of the following format :
You can see the URL is really hard to memorize as it contains query strings .
If you have a user registration website where users create their profile then the original structure of their profile URLs might look like the one I mentioned above.
It’s long but the length of URL doesn’t matter what matters the most is the structure of URLs.
Search engines like Google and Yahoo can not index those links properly as these links are externally pointing to the same php file.
You might sometimes get a “duplicate contents err” from search engines from those type of URLs.
To fix this what we can do is rewrite those URL to make them SEF URLs.
With RewriteRule , you can convert the URL above into a clean and short one. You can change that URL to
https://example.com/user/foobar/profile or even more shorter https://example.com/foobar/profile .
These are the unique and memorable URLs .
How to convert a long URL in short one using RewriteRule
You can use your htaccess file for this.
The following are the steps to create friendly URLs using htaccess file :
- Create an .htaccess file in your document root folder if it doesn’t already exist.
- Verify your htaccess and mod-rewrite. You need to make sure your server is reading the htaccess file and the rewring module is enabled.
To verify it’s working , put some random texts in the htaccess file , and visit your site. If you receive a 500 internal server error then everything is ok and you can proceed to the next step. If the htaccess or mod-rewrite isn’t enabled then you won’t see any error vising your site with random texts in htaccess.
You can enable mod-rewrite on your server just by changing a single line in your server.cofig file. Open that file and find the line AllowOverride none . Change this line to AllowOverride all . This enables the URL rewriting module to be used in an htaccess file.
In case you are on a shared hosting and you don’t have access to the configuration file , you can ask your hosting provider to enable it for you.
- Now create your rule to shorten the URL.
Assuming our original URL is :
https://example.com/users.php? username=foobar& action=profile
and we want our new URL as :
We will need to map the URL to the old one. By URL mapping here I mean “the new URL will redirect to the old one while preserving the new URL path” .
We can write a rule in our htaccess file to achieve this.
RewriteEngine on RewriteRule ^user/foobar/profile$ /users.php?username=foobar& action=profile [L]
The rule does what I mentioned above. But the problem with this is that it only applies to a single user URL.
To make the rule dynamic for all users , we can use a regex based pattern in our rule to so that it can match any user path.
RewriteEngine on RewriteRule ^user/(.+)/(.+)/?$ /users.php?username=foobar& action=profile [L]
Now it’s perfect.
The rule applies to multiple users at the same time.
The rule works when you type the new URL example.com/user/foobar/profile into your browser address bar.
A request for example.com/user/foobar/profile is then internally forwarded to the original file /users.php .
The first path segment /user is just an indetifier .
The second path segment /foobar is the name of user. The RewriteRule automatically pick it up from the URL using (.+) pattern and saves the value in $1 var so that it can be used in the destination file . We use the $1 username value to fatch usernames from “username=foobar” query perameter.
The third segment “/profile” is matched by the second capture group (.+) in our pattern . The captured value is saved in $2 so that we can use it to retrieve the “action=profile” query perameter.
The rule is as simple as ABC . I hope you learned something interesting from this post.