convert a long URL into short using RewriteRule in htaccess


URL rewriting on Apache server is one of the most easiest and convenient way to shorten a long URL to make it short and search engine friendly.
Apache server supports server.config and .htaccess files .
Not all web hosting providers allow you to access the main server.config file and in that case the only option to rewite URLs is htaccess . You can edit your htaccess file to add URL shortner rules into it.
In this post I will show you how to shorten a long PHP URL to short one by htaccess.

Shorten a long URL by htaccess

Suppose we have a user site that generates user URLs in the following format :

https://example.com/?user.php?user=john
As you can see our URL is long,hard to remember and not search engine friendly. This type of URL can cause duplicate content issue on search engines.
Now look at the following URL :
https://example.com/users/john
Does it look short and nice? Yes.. the second one makes sense as it's short and search engine friendly.

To convert your original URL into this new URL format you will need to write a Rule in your htaccess that can map the new format to the old one .
RewriteRule to map /users/john to /user.php?user=john :
RewriteEngine on
RewriteRule ^users/john$ /user.php?user=john [L]
Ok..so what the above rule does is it rewrites the requested URL path /users/john to /user.php?user=john . The rule tells your web server to make an internal redirection from new URL format to the old one and since the redirection is performed internally your browser won't notice it and the typed URL won't change in the browser address bar.

Now you can test your new URL format by going to the following URL :
https://example.com/users/john
Isn't it interesting?

The rewiteRule I mentioned above applies only to a single user URL /user.php?user=john.
What if you have more the one user ? Would you need to write multiple rules to handle them?
No.
If your user.php path is dynamic, you can use a dynamic regex pattern.
The RewriteRule bellow applies to any user path.
RewriteEngine on
RewriteRule ^users/(.+)$ /user.php?user=$1 [L]
As you can ,we slightly modified the rule and replaced the static "UserName" with a dynamic catch-all regex capture group (.+) . The regex capture group captures any character(s) after /users/ in the request path and saves the value in $1 variable.

With the rule above if you visit /users/foobar then the rule will dynamically rewite the path /users/foobar to /user.php?user=foobar .

Now that our user URL is short and SEO friendly, but there is still one major thing that can prevent the short URL from being indexed by search engines. Now you can visit the user.php URL either by going to /users/foobar or by typing the old format /user.php?u=foobar . This will cause a duplicate contents issues on search engines as Two diffrent URL paths point to the same location.
To fix this, you can write a new rule that will 301 redirect your longer URL to the shorter one.
RewriteEngine on

#301 redirect /user.php?user=foobar to /users/foobar
RewriteCond %{THE_REQUEST} /user\.php\?user=([^\s]+) [NC]
RewriteRule ^user\.php$ /users/%1? [R=301,L]


Here is the complete rule to shorten the URL :
RewriteEngine on

#1 301 redirect /user.php?user=foobar to /users/foobar
RewriteCond %{THE_REQUEST} /user\.php\?user=([^\s]+) [NC]
RewriteRule ^user\.php$ /users/%1? [R=301,L]
#2 map /users/foobar to /user.php?user=foobar
RewriteRule ^users/(.+)$ /user.php?user=$1 [L]



Hope you enjoyed reading the post! Thank you so much.

Post a Comment

0 Comments