Link to home
Start Free TrialLog in
Avatar of Pedro Chagas
Pedro ChagasFlag for Portugal

asked on

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
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

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.
Is this a CMS, like Joomla or WordPress?
Avatar of Pedro Chagas

ASKER

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
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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)
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
so article.php?id="number_of_article" is your permalink
you cannot change anything.
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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