Semantic URL htaccess Tutorial SEO Friendly and Clean Links
In this video exercise we demonstrate rewriting Semantic URLs with htaccess for improved SEO and improved user experience. They are sometimes called Clean URLs, SEO friendly links, or Pretty URLs. My hope is to pass along enough insight into how it works so that everyone can customize all of their own clean URLs by themselves, no matter how many variables the system needs to take in from the URL.
.htaccess
# Turn Rewrite Engine On
RewriteEngine on
# Rewrite for projects.php
RewriteRule ^custom$ projects.php [NC,L]
# NC makes the rule non case sensitive
# L makes this the last rule that this specific condition will match
# $ in the regular expression makes the matching stop so that "customblah" will not work
# Rewrite for user.php?u=xxxxx
RewriteRule ^user/([0-9a-zA-Z]+)$ user.php?u=$1 [NC,L]
# Rewrite for article.php?id=1&title=Title-Goes-Here
RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+)$ article.php?id=$1&title=$2 [NC,L]
user.php
<?php
echo "Username: ". preg_replace('#[^0-9a-z]#i', '', $_GET['u']);
?>
article.php
<?php
echo "Article ID: ". preg_replace('#[^0-9]#', '', $_GET['id']);
echo "<br>";
echo "Article title: ". preg_replace('#[^0-9a-z_-]#i', '', $_GET['title']);
?>