Implementing Apache Friendly URL with .htaccess and PHP

Views: 2.260
Reading Time: 5 minutes

Undoubtedly, SEO (Search Engine Optimization) is one of the main study factors for web developers today as it is directly responsible for how the website will attract new visitors and for positioning it on search engines like Google, Yahoo, Bing and others.

Some time ago, web developers had no concern about the semantics of their page URLs and it was normal to find something like http://www.site.com/noticia.php?id=587. With the advancement of SEO and the improvement of search engines, the concept of friendly url, which consists of URLs in the format http://www.site.com.br/noticia/587/url-semantica-vira-tendendia-no-mercado-web or same http://www.site.com.br/url-semantica-vira-tendendia-no-mercado-web.

In addition to making more sense for the user himself, who can get an idea of ​​the content of the link even before clicking, search engines can use the URL itself to begin indexing the content and compare the URL to the page title. and the H1 tag to make sure everyone is talking about the same subject.

In this post I will help you use this feature in your projects.

Preparing Requirements Steering

The first part of the friendly URL implementation is to direct requests to a file in our system to handle those requests, which in theory do not exist (there is no file called url-semantic-turn-on-web-market in your project). )

In the code below, I prefixed some extensions not to handle using the url_amigavel.php file, which are file extensions and certainly do not use friendly url. In this case, if any files in these extensions do not exist, they will be treated using the ErrorDocument, which I explained in the post. HTTP error redirection using .htaccess

.htaccess

Preparing Request Handling

At this stage, the client will have requested our friendly URL, which will be directed to our PHP file which will handle the URL.

url_amigavel.php

Example url: www.yoursite.com/subcategory/20/tv-lcd
Return Example: Array ([0] => Subcategory [1] => 20 [2] => tv-lcd)

Displaying content in route files

In the case of the example above, the file subcategory.php is my route file that was called through the file url_amigavel.php which will query the database and display the information in the browser to the user. Let's see how our file will identify the informed parameters:

subcategory.php

Generating the Friendly URL to display on the site

Once the structure is all set to work with friendly URL, comes the main part, which is to change the URLs of your website by the friendly URL format.

If you develop using the MVC design pattern, this will not be too much work. Just create a function on each of your models with code similar to this one, where the URL is composed of the object ID and the name (which can be title, description, finally… what you use in your project to name the object)

Subcategory.php (Model)

If you still develop in a structured way (Study Object Orientation and MVC), you will have to create a function and change your entire site so that whenever you type a URL in your final HTML, it will use this function for that.

Example:

Function used to format the name to be Friendly URL format

What about the URL www.yoursite.com/tv-lcd?

In this post, I mentioned a very common friendly URL type too, which is the format www.yoursite.com/tv-lcd for example. This kind of user-friendly URL is a bit trickier to implement since no URL ID was provided for you to query straight into the database, just text.

For this implementation, we will need to create a field in the database to store the generated friendly URL, allowing us to query our database by title (tv-lcd) and thus identifying which URL this title is associated with.

Usually a table is created for this, containing the friendly URLs and their pages that will be displayed (using include or require in PHP), as there are many different types of content (subcategories, products, categories, news, etc.) and this approach would not be restricted to just one content type.

Another (easier) approach would be to include the content ID and type at the end of the URL in the format www.yoursite.com/tv-lcd-subcategory-20 and when dealing with the request of your subcategory.php file you would make this change:

Using what you learned in this post, you can implement this smoothly. If you have questions, leave your comment and I will answer it.

Final considerations

Implementing friendly URLs in your project will not be as simple and fast as some SEO changes usually are. It's really a little work and costly, especially if you're not used to the object-oriented paradigm and MVC design pattern.

The concept of friendly URL is extremely important for SEO, and it is increasingly common to find websites that use this concept. Anyone who hasn't joined this yet (friendly URL has been a reality for a few years now) is already well behind the competition, so don't waste time.