Friendly URLs with Apache mod_rewrite, Complete SEO Guide

Friendly URLs with Apache mod_rewrite, Complete SEO Guide
Reading time: 3 min read
Link copied!

Hey Everyone!

Today a friend asked me about Friendly URLs, so I decided to write this post!

In SEO techniques, one indisputable thing is friendly URLs.

What are Friendly URLs?

Friendly URLs are normally simple and short URLs, which become more readable for search bots. Besides making understanding easier for users.

Practical Comparison

Normal URL:

http://www.xalexandre.com.br/index.php?menu=news&title=Friendly-Url&id=10

Friendly URL:

http://www.xalexandre.com.br/news/Friendly-Url/10

In the example above, the variables menu, title and id are unnecessary when using Apache mod_rewrite.

What is Mod_Rewrite?

Mod_rewrite is a module written for the Apache server, which has the function of rewriting URLs.

Apache Activation

To enable it you must first have access to the Apache server, or ask the Server Administrator to enable it.

You should edit httpd.conf adding the following line:

LoadModule rewrite_module modules/mod_rewrite.so

Just restart Apache for the module to take action.

.htaccess Configuration

After Apache is correctly configured, you must create a .htaccess file to configure our Friendly URLs.

Create a file called .htaccess (with the dot in front hidden).

Basic .htaccess Content

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php

Directive Explanation

  • RewriteEngine on → Enables the Rewrite module
  • RewriteCond %{SCRIPT_FILENAME} !-f → Checks if the typed URL is a file
  • RewriteCond %{REQUEST_FILENAME} !-d → Checks if the typed URL is a directory
  • RewriteRule ^(.*)$ index.php → Sends everything that’s not a file or directory to index.php

Testing Configuration

Save the file and test accessing a non-existent URL:

http://www.xalexandre.com.br/testing

If it redirects to index.php, Mod_Rewrite is working correctly.

PHP Processing

Now let’s edit our index.php to process URLs:

<?php
$url = explode("/", $_SERVER["REQUEST_URI"]);
?>

Practical Example

With URL: http://www.xalexandre.com.br/news/Friendly-Url/10

Our array will be:

<?php
echo $url[0]; // news
echo $url[1]; // Friendly-Url
echo $url[2]; // 10
?>

Integration with Existing System

If you have a system that depends on variables menu, title and id, do the following:

<?php
$menu = $url[0];
$title = $url[1];
$id = $url[2];
?>

Done! Your system will be working the way it was programmed with Friendly URLs.

Friendly URLs bring several advantages: better indexing by search engines, visible keywords in URL, clear hierarchical structure and better ranking in results. For users, they offer memorable and shareable URLs, intuitive navigation and enhanced experience.

For more advanced configurations, you can use specific rules like:

RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/?$ index.php?menu=$1&title=$2&id=$3 [L,QSA]

And for debugging, visualize the URL array:

<?php
print_r($url);
?>

Remember to avoid accents and special characters in URLs, use hyphen instead of underscore, keep everything lowercase and remove unnecessary stopwords.

That’s all for today everyone! Friendly URLs are essential for a modern and SEO-optimized website. Questions, I’m available.