What are permalinks? Actually, they are just various links to entries of your website. Long ago, nearly all hyperlinks were permalinks, just because all content was static (static html pages). Later, with the development and popularization of programming languages, dynamic links began to appear. As example: dynamic listings of members, blogs, photos, etc. We ourselves can determine the permalinks scheme, in order to make them more friendly. As always, Rewrite engine (mod_rewrite module) is used to convert urls with different GET params into more human-readable urls.
In the beginning, I recommend that you check if ‘mod_rewrite’ is installed on your Apache server (phpinfo)
[sociallocker]
[/sociallocker]
Now you can download the package to follow my explanations.
Step 1 – .htaccess
Firstly, create an empty .htaccess file and paste next code:
01 | <IfModule mod_rewrite.c> |
04 | RewriteRule ^home/{0,1}$ index.php?module=home [QSA,L] |
05 | RewriteRule ^faces/{0,1}$ index.php?module=faces [QSA,L] |
06 | RewriteRule ^clubs/{0,1}$ index.php?module=clubs [QSA,L] |
07 | RewriteRule ^photos/{0,1}$ index.php?module=photos [QSA,L] |
08 | RewriteRule ^videos/{0,1}$ index.php?module=videos [QSA,L] |
09 | RewriteRule ^blog/{0,1}$ index.php?module=blog [QSA,L] |
11 | RewriteRule ^home/([^/.]+)/{0,1}$ index.php?module=home&key=$1 [QSA,L] |
12 | RewriteRule ^faces/([0-9]+)/{0,1}$ index.php?module=faces&value=$1 [QSA,L] |
14 | RewriteRule ^clubs/([^/.]+)/([0-9]+)/{0,1}$ index.php?module=clubs&key=$1&value=$2 [QSA,L] |
15 | RewriteRule ^photos/([0-9]+)/([0-9]+)/{0,1}$ index.php?module=photos&value1=$1&value2=$2 [QSA,L] |
17 | RewriteCond %{REQUEST_FILENAME} -f [OR] |
18 | RewriteCond %{REQUEST_FILENAME} -d |
20 | RewriteRule ^([^/]+)/{0,1}$ index.php?module=other&key=$1 [QSA,L] |
Then, save this file, and upload to the root folder of your project (website). As usual, this is ‘www’ or ‘httpdocs’ folder. Now, let’s back to our rules:
All rules are wrapped with ‘IfModule mod_rewrite’ construction. This is some kind of check if ‘mod_rewrite’ is installed or not. After, I added different permalink rules to different section of our ‘website’ (see – demo page). There are several main sections: home, faces, clubs, photos, videos and blog. Also, there are 2-level and 3-level constructions (links).
In the beginning of every rule you can see ^ anchor. It means – ‘Start-of-line anchor’. Anchor ‘$’ – ‘End-of-line anchor’.
Next flags are used:
‘QSA’ (query string append) – this flag forces the rewrite engine to append a query string part of the substitution string to the existing string, instead of replacing it. Use this when you want to add more data to the query string via a rewrite rule.
‘L’ (last rule) – stop the rewriting process here and don’t apply any more rewrite rules. This corresponds to the Perl last command or the break command in C. Use this flag to prevent the currently rewritten URL from being rewritten further by following rules.
Pay attention, that we can use regular expressions in our rules, like [0-9]+) or ([^/]+). The first expressions is ‘any digit (any length)’, the second is ‘any symbol (any length)’.
Note, that in order to pass params into GET variables, we have to wrap necessary sections in round brackets. The first section will be accessible through ‘$1’ key, second – ‘$2’, and so on.
Step 2 – HTML
in order to give our demo finished look, I prepared a template of our page
templates/index.html
004 | <meta charset="utf-8" /> |
005 | <meta name="author" content="Script Tutorials" /> |
006 | <title>Permalinks tutorial | Script Tutorials</title> |
007 | <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> |
008 | <meta content="width=device-width, initial-scale=1" name="viewport" /> |
010 | <base href="{url}" /> |
012 | <link href="css/main.css" rel="stylesheet" type="text/css" /> |
013 | <link href="css/responsive.css" rel="stylesheet" type="text/css" /> |
018 | <a href="#" id="logo"><img src="images/logo.png"></a> |
021 | <a href="about-us/">About us</a> |
022 | <a href="help/">Help</a> |
023 | <a href="contact-us/">Contact Us</a> |
028 | <input type="checkbox" id="menu_check"> |
029 | <label for="menu_check" onclick> </label> |
031 | <li {mact1}><a href="home/">Home</a> |
033 | <li><a href="home/facebook/"><span class="icon elem1"></span>Facebook</a></li> |
034 | <li><a href="home/google/"><span class="icon elem2"></span>Google</a></li> |
035 | <li><a href="home/rss/"><span class="icon elem3"></span>RSS</a></li> |
036 | <li><a href="home/skype/"><span class="icon elem4"></span>Skype</a></li> |
037 | <li><a href="home/stumbleupon/"><span class="icon elem5"></span>Stumbleupon</a></li> |
040 | <li {mact2}><a href="faces/">Faces</a> |
042 | <li><a href="faces/1/"><span class="icon elem0"></span>Faces 1</a></li> |
043 | <li><a href="faces/2/"><span class="icon elem0"></span>Faces 2</a></li> |
044 | <li><a href="faces/3/"><span class="icon elem0"></span>Faces 3</a></li> |
045 | <li><a href="faces/4/"><span class="icon elem0"></span>Faces 4</a></li> |
046 | <li><a href="faces/5/"><span class="icon elem0"></span>Faces 5</a></li> |
049 | <li {mact3}><a href="clubs/">Clubs</a> |
051 | <li><a href="clubs/step/1/"><span class="icon elem0"></span>Clubs 1</a></li> |
052 | <li><a href="clubs/step/2/"><span class="icon elem0"></span>Clubs 2</a></li> |
053 | <li><a href="clubs/step/3/"><span class="icon elem0"></span>Clubs 3</a></li> |
054 | <li><a href="clubs/step/4/"><span class="icon elem0"></span>Clubs 4</a></li> |
055 | <li><a href="clubs/step/5/"><span class="icon elem0"></span>Clubs 5</a></li> |
058 | <li {mact4}><a href="photos/">Photos</a> |
060 | <li><a href="photos/10/10/"><span class="icon elem0"></span>Photos 10</a></li> |
061 | <li><a href="photos/20/10/"><span class="icon elem0"></span>Photos 20</a></li> |
062 | <li><a href="photos/30/10/"><span class="icon elem0"></span>Photos 30</a></li> |
063 | <li><a href="photos/40/10/"><span class="icon elem0"></span>Photos 40</a></li> |
064 | <li><a href="photos/50/10/"><span class="icon elem0"></span>Photos 50</a></li> |
067 | <li {mact5}><a href="videos/">Videos</a></li> |
068 | <li {mact6}><a href="blog/">Blog</a></li> |
073 | <form role="search" method="get"> |
074 | <input type="text" placeholder="search..." name="s" value="" autocomplete="off" /> |
085 | <h3 class="head">{title}</h3> |
089 | <h3 class="foot">Footer</h3> |
095 | <h3 class="head">{title}</h3> |
097 | Some extra content. Some extra content. Some extra content. Some extra content. |
099 | <h3 class="foot">Footer</h3> |
There are only several template keys in use: {url} (to set a base url), {mact1} – {mact6} (menu active element status), {title} and {explanation} (to place title and some explanation).
Step 3 – PHP
index.php
Finally, the code of our main homepage:
02 | $sMac1 = $sMac2 = $sMac3 = $sMac4 = $sMac5 = $sMac6 = ''; |
03 | $sTitle = $sText = 'Home'; |
05 | switch($_GET['module']) { |
07 | $sMac1 = 'class="active"'; |
08 | $sText = 'RewriteRule ^home/{0,1}$ index.php?module=home [QSA,L]'; |
09 | if (isset($_GET['key']) && $_GET['key']) { |
10 | $sTitle .= ' - ' . $_GET['key']; |
11 | $sText = 'RewriteRule ^home/([^/.]+)/{0,1}$ index.php?module=home&key=$1 [QSA,L]'; |
15 | $sMac2 = 'class="active"'; |
17 | $sText = 'RewriteRule ^faces/{0,1}$ index.php?module=faces [QSA,L]'; |
18 | if (isset($_GET['value'])) { |
19 | $sTitle .= ' - ' . (int)$_GET['value'] . ' page'; |
20 | $sText = 'RewriteRule ^faces/([0-9]+)/{0,1}$ index.php?module=faces&value=$1 [QSA,L]'; |
24 | $sMac3 = 'class="active"'; |
26 | $sText = 'RewriteRule ^clubs/{0,1}$ index.php?module=clubs [QSA,L]'; |
27 | if (isset($_GET['key']) && isset($_GET['value'])) { |
28 | $sTitle .= ' - ' . (int)$_GET['value'] . ' ' . $_GET['key']; |
29 | $sText = 'RewriteRule ^clubs/([^/.]+)/([0-9]+)/{0,1}$ index.php?module=clubs&key=$1&value=$2 [QSA,L]'; |
33 | $sMac4 = 'class="active"'; |
35 | $sText = 'RewriteRule ^photos/{0,1}$ index.php?module=photos [QSA,L]'; |
36 | if (isset($_GET['value1']) && isset($_GET['value2'])) { |
37 | $sTitle .= ' - ' . (int)$_GET['value1'] . ' : ' . (int)$_GET['value2']; |
38 | $sText = 'RewriteRule ^photos/([0-9]+)/([0-9]+)/{0,1}$ index.php?module=photos&value1=$1&value2=$2 [QSA,L]'; |
42 | $sMac5 = 'class="active"'; |
44 | $sText = 'RewriteRule ^videos/{0,1}$ index.php?module=videos [QSA,L]'; |
47 | $sMac6 = 'class="active"'; |
49 | $sText = 'RewriteRule ^blog/{0,1}$ index.php?module=blog [QSA,L]'; |
52 | if (isset($_GET['key'])) { |
53 | $sTitle = 'Other - ' . $_GET['key'] . ' (or, error 404, not found)'; |
54 | $sText = 'RewriteRule ^([^/]+)/{0,1}$ index.php?module=other&key=$1 [QSA,L]'; |
59 | $sMac1 = 'class="active"'; |
71 | '{explanation}' => $sText, |
73 | echo strtr(file_get_contents('templates/index.html'), $aKeys); |
As you see, depending on params – this code generates different titles and explanation texts for our template. Also it passes the base ‘website’ url.
Conclusion
That’s all for today. I hope, that from now on you are able to apply your knowledge in practice in order to build friendly permalinks for your own projects. Good luck and welcome back.