Mod_rewrite - Information from data base

Hi E's,
I never used apache mod_rewrite before. The Internet have lot's of simple examples, easy to understand, but no example shows the solution to my problem.
I have a project, with lot's of pages, and the main part is articles. The URL of articles looks like:
mydomain.com/articles.php?id=number
. I want to create friendly URLs for each article.
For that end, I create a control panel, where I save in my data base (Mysql) the articles, and I save also the name of the friendly URL, so, in my data base I keep the article and the name of friendly URL for specific article, and for each article I create a friendly URL.
If I have just 3 or 4 articles, I edit the file by hand. But are dozens of articles, and I would make the system work automatically for friendlies URL's.

Any idea?

The best regards, JC
LVL 4
Pedro ChagasWebmasterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Apache mod_rewrite is processed before the PHP page is started.  You might be able to write out a new .htaccess for next time but not for the current request.
0
Ray PaseurCommented:
Is this a CMS, like Joomla or WordPress?
0
Pedro ChagasWebmasterAuthor Commented:
Hi @Ray_Paseur, I not use a CMS.
I have my own resources. I write the project in php (entire), and the project are located in my server.
As I said, I save all articles through a control panel into Mysql data base, and one of the informations I send to the db is the name of the friendlies URL.

Hi @Dave Baldwin, in other words, can be said that it is a very difficult task?

~JC
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Dave BaldwinFixer of ProblemsCommented:
No, it is impossible.  The 'mod_rewrite' occurs before the PHP page is called and as far as I know, Apache has no ability to lookup the URL in a database.
0
Pedro ChagasWebmasterAuthor Commented:
Ok. thanks for the clarification.
I have a second idea to solve the problem, but do not know if it is a good practice:
Every time I create a new article, I increase a new lines in file .htaccess!?
What is your opinion about my new idea?

~JC
0
Dave BaldwinFixer of ProblemsCommented:
I'm sure some people have done it that way.  The reason that Ray asked about a CMS is that they rewrite the URLs so they can pass it in a query string to 'index.php' which acts as a 'dispatcher' to send the user the right data or page.  The 'index.php' can use the database to look up the desired response.

Read thru this article from Wordpress to see if you can use any of the info there:  http://codex.wordpress.org/Using_Permalinks
0
GaryCommented:
What is the format of your friendly urls

In your htaccess you can route calls for non existent files to a router page where you just parse the url and serve the correct article
You never need to change the htaccess, and all new url's will be automatically taken care of
0
gheistCommented:
You are looking for "permalink" solution. That definitely requires database (and since you use PHP - a PHP script handling all friendly URLs and redirecting accordingly)
0
Pedro ChagasWebmasterAuthor Commented:
Hello again,
Yes, I'm looking for a "permalink" solution, and I will need help to begin.
The structure of my site/blog is very simple, and just have a first level of links:
index.php (main page)
support.php (page of support or contact us)
article.php?id="number_of_article" (each article have a different name, inventing, and stored in the database next to each document)

If not allowed to invent new names (for friend links) without any mathematical logical, can you please advise me about the best way to the links seems friendly and respect SEO rules.
I hope everyone can help

~JC
0
gheistCommented:
so article.php?id="number_of_article" is your permalink
you cannot change anything.
0
Pedro ChagasWebmasterAuthor Commented:
I'm confused.
article.php?id=number (example article.php?id=10), is the natural link. Maybe don't have solution for transform this natural link to something like: mydomain.com/the_name_of_article/ ?
If not possible, what you suggest, for I present more friendly links?

~JC
0
GaryCommented:
There's quite a bit of work to do this yourself and may require you to open multiple questions to get the full answers for each part if you have problems but here's the logic and workflow

In your db you will need something like this plus any other columns you need, you said you already have a friendly url column

ID	Article_Title		Article_Description	Friendly_URL
1	My Article Title 1	About this article	my_article_title_1

Open in new window

You can use this function to take the Article_Title and create a friendly url from the title. You will also need some logic to check for duplicate article titles and append the url with a counter for example

function prettyurl($article_title){
	return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($text, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
}

Open in new window

When you show links to your articles you then use the format
/article/my_article_title_1
How you do this will depend on your code but basically you are just getting the friendly_url for the link and not the ID

Add to your .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /router.php [L]

Open in new window

This will redirect all requests for pages that don't exist to router.php

In router.php you will have something like this

<?php
$url=$_SERVER['REQUEST_URI'];
$article_name=explode("/",$url);

if($article_name[1]=="article"){
// Add your db code to select and display the article from the db based on $article_name[2]
// which in this example would be my_article_title_1

}
else{
// Not an article URL, probably a real 404 error or wrong article url.
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pedro ChagasWebmasterAuthor Commented:
Excelent @Gary, excellet solution, thanks.
But, I just don't undertand the function prettyurl, can you explain please:
    function prettyurl($article_title){
        return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($text, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
    }
    $link = "this is the title of the article";
    echo prettyurl($link);

Open in new window

The return it's a empty string.

~JC
0
GaryCommented:
Change it to
    function prettyurl($article_title){
        return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($article_title, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
    }

Open in new window

I was copying my code and changing the function to make it more relevant but forgot to change it inside the function itself
0
Pedro ChagasWebmasterAuthor Commented:
I'll get in your solution, and begin to explore and adapt to my case. Maybe I will open new questions related to this subject.
Thank you.

The best regards, JC
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.