Papermashup

Subscribe


Tweets


"RT @bethgordon: .@Tesco Please rethink archaic 'Approved by Mums'. Pretty sure it's not just mothers who make childcare decisions. https://…"

@ashleyford 2 hours ago

"Is there ever a time that @DFS doesn't have a sale on?"

@ashleyford 4 days ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Papermashup

Create a URL from a string of text with PHP and mod_rewrite

A PHP function to generate URL slugs

AshleyAshley

Quite often when developing big web apps you may need to use mod_rewrite to generate SEO friendly URLs from a string of text, Very similar to the way WordPress generates URLs for blog posts.

The Code

Below is the function which will remove all the spaces and special characters from a string and convert it to lowercase making sure it’s formatted ready to be used as a URL.



function create_slug($string == null){

// if no string
if($string == null){
return false;
}

$string = preg_replace( '/[«»""!?,.[email protected]$%^&*{};:()]+/', '', $string );
$string = strtolower($string);
$slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);

return $slug;
}

echo create_slug('This is my page title');

// this should print out: this-is-my-page-title

The .htaccess

If you were to use the function above in conjunction with generating page urls as part of a web application you’ll need some way of the server handling url redirects etc. The easiest way to do this is by using .htaccess. More information can be found about .htaccess here. What the code below does is, it takes the url for example ‘http://papermashup.com/this-is-my-page-title’ then appends the; ‘this-is-my-page-title’ as a GET variable value, in the case below we’re saving it in the GET variable ‘slug’ which is appended to index.php. So now when we visit ‘http://papermashup.com/this-is-my-page-title’ the server looks for http://papermashup.com/index.php?slug=this-is-my-page-title. Then it’s up to you to process the data for each url. So if you had a web app you might have a MySQL database that holds all your site URLs and data which you search based on the page slug.



RewriteEngine on
RewriteRule ^([a-zA-Z0-9-/]+)$ index.php?slug=$1 [QSA]

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Comments 6
  • Rever Eye
    Posted on

    Rever Eye Rever Eye

    Reply Author

    Nice post,Thank you.


  • Enamul Hoq
    Posted on

    Enamul Hoq Enamul Hoq

    Reply Author

    Great tips URL creating so helpful information thanks for sharing


  • Ezequiel
    Posted on

    Ezequiel Ezequiel

    Reply Author

    Nice! You can use [^A-z0-9-] ER. :)


  • Chiel Kunkels
    Posted on

    Chiel Kunkels Chiel Kunkels

    Reply Author

    @DaveBowman: that stands for “Query String Append”, which means it mingles in additional GET parameters with the rest of the things you pass from the htaccess.


  • Dhanunjaya Rao
    Posted on

    Dhanunjaya Rao Dhanunjaya Rao

    Reply Author

    it’s working


  • DaveBowman
    Posted on

    DaveBowman DaveBowman

    Reply Author

    Nice tip, but what’s the [QSA] directive at the end of the rewrite rule?