Snippets

Here's some of my tips, tweaks and tuts, ranging from PHP, Linux, MySQL and more!

Snippets / Apache

Hide php extension with Apache

  • Snippet Length: 10 minutes
  • Difficulty: Easy
  • Knowledge: PHP, Linux Administration, Vim Editor

Ever wanted to get some pretty URLs without ever showing the file extension of the php

1. First step is to create a .htaccess in the root of your website if theres not one already there we can create it via the following line

sudo vim .htacesss

2. Once inside the file we will need to turn the apache mod re-direct engine on

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>

3. Save the .htaccess

:wq!

4. You can now try created a file called test.php in the base of your web folder and see if its reachable by accessing www.yourdomain.co.uk/test if you recieve

13 years ago / Read More

Snippets / Apache

SEO friendly URLs with apache

  • Snippet Length: 15 minutes
  • Difficulty: Easy-Intermediate
  • Technologies: PHP, Linux System Administration, Apache

Here is my take on the popular apache mod re direct;

So your wanting your links to look readable by humans and search engines such as ;

www.yoursite.com/news/19/latest-update-notes.html instead of

www.yoursite.com/news/index.php?newsId=19&newsTitle=latest%20update%20notes

One way of doing this is telling Apache that when it encounters a certain url that is passed look for a different page.

Starting Point...

1. You can start by creating a .htaccess config in the root directory of the site via SSH;

nano .htaccess

2.Inside this .htaccess file enter the following below;

RewriteEngine on
RewriteRule ^news/([0-9]+)/(.*?).html$ news/index.php?id=$1

Lets break this down; "RewriteEngine On" basically just turns the setting on for this domain so that we can use the redirect ( pretty simple ).

The second line creates a rule that the directory we can look at is ^news/ so that would be /news/.

The next part /(0-9+)/ will only accept integers and wil be a virtual directory, this will be the id of the news item which is dynamically generated.

The next part (.*?).html$ will allow any text preceding .html which is a fixed element or the url. The $ dollar sign ends the first argument of the 2nd line.

Save & Restart Apache...

3.Save the config file (CTRL + X) and y to confirm then hit enter.

4.Restart apache – it depends what version of linux your using most of the time you can just do

apachectl -k graceful

Conclusion...

There's also a big debate as to whether or not search engines actually rank web pages higher because of the .html ending, I tend to stick with it because some research does suggest that search engines rank static content quite highly ( but this is constantly changing )

However...

13 years ago / Read More