Blog | Tutorial

Permalink web addresses
[TITLE]

I`ve been implementing and updating websites for some considerable years using the standard URL addresses for accessing content.

I`d heard of the newer methods for enhancing the addresses in particular helping with SEO. SO it was time to look into how this is done. The image shown shows `award-winning-products` as the permalink address from [the Q Guild of Butchers ] website.
Traditionaly the web addresses I was using names and data on the URL, what that mean`s is that the name of the parameters are being used for passing information to the webpage.

An example with that format is:


https://dafc.co.uk/index.php?SID=First+Team&D=2018-08-18&ID=1172
or
https://dafc.co.uk/index.php?c=News&sc=The+Boardroom


The first one passes three values SID, D and ID while the second one passes a category (c) and subcategory (sc).

Nowadays these should be simplified and shown as :

https://dafc.co.uk/fixture/first-team/2018-08-18/1172
and
https://dafc.co.uk/news/the-boardroom


The issue now then is to how to change the website design and the server to be able to use this new format.

Web site address


But firstly lets have a review of the current method before going onto how the new method will work. The traditional method of setting up a website and links passes parameters into the new web page on the URL. Normally the field name is used as the reference along with the actual value. For example `index.php?ID=2718` means the value of 2718 ias passed is as the ID field (thi svalue is then used to look up a database and the data from that entry is then shown on the page.

This means that the script file index.php needs to get the input value for the variable ID which in this case is 2718. This means that the script file is known to the viewers of the wbesite (perhaps a security issue as hackers could use that as a means of trying to break in ? - but then there are other features you need to recognise to block hackers). Overall the URL itself is not very readable or memorable.

With the newer permalink url address it is much simpler and easier to understand as well as hiding the underlying implementation mechanisms. Also it is more search engine friendly.

URL redirection

normally the web address contains the name of the script file that will be used to load the page. For example one of the pages above `index.php` will be used to load the page.

The term permalink means the full fixed address for the web page, more can be found on the link. The PHP scripts now need to be modifed to be able to handle this permalink.

The normal default pages used when loading a web page are index.html, index.php or some other filename specified in the apache configuration. My default filename is always `index.php`.

Therefore to be able to handle the new permalink we need our `index.php` file to be able to handle these input parameters. The way I`ve chosen this is to use the default file `index.php` to handle the input parameters. I will also have separate handlers to handle the home page, the news list page and the unique story page (there are many more options so will just keep it simple.

.htaccess file

First thing we need is for apache to be able to handle the permalink addresses without amending the address bar link. This is handled by the `.htaccess` file.


RewriteEngine on

RewriteCond $1 !^(index\.php|(.*)\.swf|fonts|images|css|icon|pdf|files|files_220|files_th|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]

Suffice to say I`m not going to explain this as `.htaccess` files are a story book in their own right.

Normally the web address would be https://domain.com/index.php?CATEGORY=news&subcategory=weather but we are now going to use the permalink format https://domain.com/news/weather

Handling the permalink input

I`m going to show some very basic code to handle the permalink inut and conver to a format that I would use for the actual script.


$URL = $_SERVER[`QUERY_STRING`];
$INPUTS = explode ("/", $URL ) ;
$NUMBER = count ($INPUTS) ;


The Server Query String returns the information from the address bar, for example: news/weather

The the explode with the URL provides the array INPUTS with the individual parameters.

$INPUTS[0] takes the string "news"
$INPUTS[1] takes the string "weather"



Then to use these in my existing code I use `category` and `subcat` as variables in code to handle the input parameters.


$CATEGORY = $INPUTS[0] ;
$SUBCAT = $INPUTS[1] ;


Thats it, that is all that is needed to know about handling permalinks ;o)


Please note that I have not included code to prevent SQL injection but is included in production software.



-- email Mark