<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SpiceMailer Web Tutotiral &#187; slugs</title>
	<atom:link href="http://spicemailer.com/web/tag/slugs/feed/" rel="self" type="application/rss+xml" />
	<link>http://spicemailer.com/web</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Thu, 19 Sep 2024 16:44:50 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.9.2</generator>
	<item>
		<title>Create unique slugs for URLs using PHP and Python</title>
		<link>http://spicemailer.com/web/create-unique-slugs-urls-using-php-python/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=create-unique-slugs-urls-using-php-python</link>
		<comments>http://spicemailer.com/web/create-unique-slugs-urls-using-php-python/#comments</comments>
		<pubDate>Wed, 17 Sep 2014 07:33:41 +0000</pubDate>
		<dc:creator><![CDATA[FrantzFerdinand]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[slugs]]></category>

		<guid isPermaLink="false">http://spoontalk.com/web/?p=40</guid>
		<description><![CDATA[<p>With every geek on Quora, it has become a place where people are asking all kinds of weird questions and that too again and again. If anyone observes the URLs, you would notice something called slugs. Slugs in the URLs are SEO friendly. By SEO friendly, it means that a URL like http://xyz.com/what-is-spice-forms is much better than [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://spicemailer.com/web/create-unique-slugs-urls-using-php-python/">Create unique slugs for URLs using PHP and Python</a> appeared first on <a rel="nofollow" href="http://spicemailer.com/web">SpiceMailer Web Tutotiral</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>With every geek on Quora, it has become a place where people are asking all kinds of weird questions and that too again and again. If anyone observes the URLs, you would notice something called slugs. Slugs in the URLs are SEO friendly. By SEO friendly, it means that a URL like <code>http://xyz.com/what-is-spice-forms</code> is much better than than an <code>id</code> based url like this <code>http://xyz.com/post/4</code>. The <code>what-is-spice-forms</code> portion in URL is called slug. It points to exactly <code>/post/4</code> and hence, acts as an alias.<span id="more-40"></span></p>
<p>Clean URLs help search engines like Google, Bing to identify the content of the page (yes they are intelligent enough to understand English) and if searched for, it simply lists pages with these URLs on the top. In fact, Google uses gives preferences to the content of the URL while ranking results &#8211; this means that if the slug contains a search term, there is a probability that it would be ranked higher. The routing system of each framework is different and URLs can be stored with a matching value. For example, a URL like <code>http://xyz.com/what-is-spice-forms</code> is actually interpreted as<code>http://xyz.com/post/4</code>. It means visiting the not-so-friendly URL will also land you to same page.</p>
<p>There might be a case of ambiguity if someone asks the same question. Usually that time a database search is done and the URL is modified slightly to make it unique. For example, it can be observed that Facebook stores URL like <code>http://xyz.com/what-is-spice-forms-12836</code>. The last number is used to make the slug unique and it reduces a database query.</p>
<p>Overall, it can be concluded that slug based URLs are very constructive towards the popularity of the site.</p>
<h3><a class="anchor" style="color: #4183c4;" href="https://gist.github.com/sdaityari/c2770d7f8c8ce112f7b9#slug-maker" rel="noreferrer" name="user-content-slug-maker"></a>PHP Slug Maker</h3>
<p>We will discuss the slug maker in PHP. It is very easy to make a slug out of a given sentence or group of words. We are going to take inspiration from a <a style="color: #4183c4;" href="https://gist.github.com/anonymous/2912227" rel="noreferrer">gist</a> and then slightly modify it to create unique URLs.</p>
<pre><code>&lt;?php
function slugify($str) {
    $search = array('Ș', 'Ț', 'ş', 'ţ', 'Ş', 'Ţ', 'ș', 'ț', 'î', 'â', 'ă', 'Î', 'Â', 'Ă', 'ë', 'Ë');
    $replace = array('s', 't', 's', 't', 's', 't', 's', 't', 'i', 'a', 'a', 'i', 'a', 'a', 'e', 'E');
    $str = str_ireplace($search, $replace, strtolower(trim($str)));
    $str = preg_replace('/[^\w\d\-\ ]/', '', $str);
    $str = str_replace(' ', '-', $str);
    return preg_replace('/\-{2,}/', '-', $str);
}
</code></pre>
<p>This function generates a slug based out of the <code>$str</code> you pass to the function. A string like this <code>The quick brown fox</code> was slugified to <code>the-quick-brown-fox</code>. To make it unique, we will add a unique string at the end.</p>
<pre><code>$url = slugify("The quick brown fox")."-".uniqid();
</code></pre>
<h3><a class="anchor" style="color: #4183c4;" href="https://gist.github.com/sdaityari/c2770d7f8c8ce112f7b9#php-uniqid" rel="noreferrer" name="user-content-php-uniqid"></a>PHP uniqid()</h3>
<p><a style="color: #4183c4;" href="http://php.net/manual/en/function.uniqid.php" rel="noreferrer">uniqid()</a> helps to generate a unique random string and the algorithm makes it almost unique. There are some params which can be passed to increase the likelihood of uniqueness but since we are already combining it with a slug, the total string being duplicate has inverse astronomical probability. In simple words, the slug generated will be unique. If you are using databasees, you can also append the primary key (ID) to the end of the string to make it unique.</p>
<h2><a class="anchor" style="color: #4183c4;" href="https://gist.github.com/sdaityari/c2770d7f8c8ce112f7b9#python" rel="noreferrer" name="user-content-python"></a>Python</h2>
<p>In python, a package already exists which makes slugs. You can install it using a package manager like pip.</p>
<pre><code>pip install python-slugify
</code></pre>
<h3><a class="anchor" style="color: #4183c4;" href="https://gist.github.com/sdaityari/c2770d7f8c8ce112f7b9#usage" rel="noreferrer" name="user-content-usage"></a>Usage:</h3>
<pre><code>txt = "This is a test ---"
r = slugify(txt) // gives 'this-is-a-test'
</code></pre>
<p>The unique ID can be attached using <code>uuid()</code> function and voila, the slug is created.</p>
<h3><a class="anchor" style="color: #4183c4;" href="https://gist.github.com/sdaityari/c2770d7f8c8ce112f7b9#python-django" rel="noreferrer" name="user-content-python-django"></a>Python Django</h3>
<p>If you are using Python Django, a package is available called <code>django-autoslug</code> which does exactly the same. Also, the <code>[SlugField](https://docs.djangoproject.com/en/dev/ref/models/fields/#slugfield)</code>may be used to create slugs in models.</p>
<p>I hope the quest for creating unique slugs is over. Keep creating them!</p>
<p>The post <a rel="nofollow" href="http://spicemailer.com/web/create-unique-slugs-urls-using-php-python/">Create unique slugs for URLs using PHP and Python</a> appeared first on <a rel="nofollow" href="http://spicemailer.com/web">SpiceMailer Web Tutotiral</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://spicemailer.com/web/create-unique-slugs-urls-using-php-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
