Friday , 22 September 2017

Introduction to Simple URL Rewriting (.htaccess)

Simple URL Rewriting

Simple URL rewriting is a technique which changes the complicated format of your website URLs to simple formation so that readers can easily understand it and spiders can fetch it easily and this is one of the best method or technique in SEO world so don’t ignore your website complicated URLs format read the rest of article hope you’ll love it and this is specially for web developers who are looking for the best way to index website URLs easily in search engines. Also read my another article to send data without reloading page hope you’ll love it too.

What is Simple URL Rewriting?

As you have read above paragraph where I have described what is simple URL rewriting and how it is useful in SEO. So I am going to share few examples with you so that you can understand it more and more. Actually for example if you have a website where you have URL format like this yourwebsite.com/index.php?id=1 this is very bad for both readers and spiders and definitely for search engines but do not worry we can make it like this yourwebsite.com/1 with the help of simple URL rewriting method. As you can clearly see that it takes less space in search results and also it looks better than that complicated format and for your information URL shortener websites use this method to show links like yourwebsite.com/skshu but in real the links are in this format yourwebsite.com/index.php?id=skshu so if you are going to make a URL shortener website then this article will help you a lot and also in any other website which you want to be SEO optimized. Also read another article to refresh DIV without reloading the page with the help of jQuery.

There is another example, if you have a website where you show category and title in the links then simple URL rewriting is best for your website. For example your website links format is like this yourwebsite.com/index.php?cat=tech&title=seo so as you can notice that there are now two GET values in the URL so we can also make it like this yourwebsite.com/tech/seo so do you like this format now? I guess you do like it! If you have more than 2 values it doesn’t matter how many GET values you have in the URL all we need to do is to write few lines of code in .htaccess file to turn all those complicated URLs into simple nice clean format URLs :)

Simple URL Rewriting With Examples

I hope now you have answer that what is simple URL rewrite and why should we use it? So, now here you can learn how to do simple URL rewrite. For example you want to convert yourwebsite.com/index.php?id=43 format into yourwebsite.com/43 then first of all make a new file with name .htaccess and then paste the code which is given below and make sure you upload this file to root directory of your web hosting account and also read the description of code that how it works given below:

1
2
RewriteEngine On
RewriteRule ^([0-9]+)/?$ index.php?id=$1 [NC,L]
  • See line no. 1 it is written to turn on the rewrite engine to make it processing and working.
  • At line number 2 you ^ means starting and & means ending and between these characters we type in that which can be written in the URLs e.g. alphabets, numeric values or both so right now we just need to type in [0-9]+ it means we can type 0-9 in the URLs and + means one or more numbers like 43 or 143 so that’s it.
  • /? means it will work without adding slash in the end of URLs or by adding it so it’s important feature to use.
  • [NC,L] means NC to tell the rewrite engine that it’s case insensitive it means user can type capital alphabets or lower alphabets it will show as user typed in but right now we are not allowing users to type in alphabets it is discussed in the next example.
  • We can use MySQL query to extract data from database and use it as GET value, I hope you understand it if you are a web developer.
  • We can test it by echoing $_GET['id'] in the index.php file so just test it now.

Now see the next example where we can show two values in the URLs like yourwebsite.com/tech/seo so let’s get started. Make .htaccess file and paste the code which is given below:

1
2
RewriteEngine On
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ index.php?cat=$1&title=$2 [NC,L]
  • You can understand almost 90% working if you’ve read my previous example. On line number 2 there is forward slash / between character checking code it means after typing first GET value user has to put /
    then the second value means yourwebsite.com/cat/title so the slash between cat and title is what I am talking about. We can change it to any sign like - or _ etc.
  • Again we can extract category and title from the database with MySQL query and can use it in simple URL rewriting.

I hope there is nothing left to teach you about this great technique so do not miss this amazing chance to learn it from here because I did my best to teach the easy way but still if you have any question about this method then comment but before asking something go here hope there will be more about it and thanks for reading it!

Leave a Reply