/    Sign up×
Community /Pin to ProfileBookmark

.htaccess with PHP

Currently, I am using .htaccess rewrite to change some of the names of my URLs. However, I want to take it to another level.

Currently, here is what I have:

RewriteEngine On

RewriteRule ^newsarticle/page/([0-9]+)/?$ newsarticle.php?id=$1

So if my news article was located at: [url]http://www.mydomain.com/newsarticles.php?id=3[/url], it will now be [url]http://www.mydomain.com/newsarticles/page/3[/url]

However, I want it a little different.
Rather than ending in with the ID number, I want the URL to have the title.

If the news article title is Cool News Article, then I want the URL to be [url]http://www.mydomain.com/newsarticles/Cool-News-Article[/url]

Does anyone know what I would have to do to accomplish this task?

p.s. All news articles are pulled from a database and the title is stored in the field called `title`.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@knowjDec 01.2007 — The way wordpress works is that it processes the title into a url friendly title (permalink) and saves this in the database (make it alphanumeric and replace the spaces with underscores)

<i>
</i>RewriteEngine on
RewriteRule ^newsarticles/([0-9a-zA-Z-_]+)/$ index.php?title=$1 [NC]


im currently working on this system for my personal blog.

thanks to nogdog for help on how to process the title into alphanumeric/underscores:
[code=php]
function permalink($value)
{
$search = array(
'/s+/' => '_', // changes all white-space characters to underscores
'/W/' => '' // remove non-word characters
)
$value = preg_replace(array_keys($search), $search, $value);
return $value;
}
[/code]
Copy linkTweet thisAlerts:
@gc40authorDec 01.2007 — I am completely lost. Can you post a complete example?
Copy linkTweet thisAlerts:
@knowjDec 01.2007 — 1: you would just put your title (when you submit a new article) through that function to strip out all non alpha numeric characters (lets now call it the permalink).

2: save the permalink into your database on the same row as your blog/news entry in a field called permalink.

3: you would then add the mod_rewrite code into the .htaccess
<i>
</i>RewriteEngine on
RewriteRule ^newsarticles/([0-9a-zA-Z-_]+)/$ index.php?title=$1 [NC]

4: your SQL code would be something along the lines of

[code=php]
$query = "SELECT * FROM news WHERE permalink='$_GET[title]'";
$results = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($results)
echo $row['title'];
//etc......
[/code]
×

Success!

Help @gc40 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...