Solved

Mod_rewrite - Information from data base

Posted on 2014-07-27
15
349 Views
Last Modified: 2014-07-28
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
0
Comment
Question by:Pedro Chagas
  • 6
  • 3
  • 3
  • +2
15 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40222947
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40222972
Is this a CMS, like Joomla or WordPress?
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 40223013
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
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 60 total points
ID: 40223035
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
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 40223069
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
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 60 total points
ID: 40223104
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
 
LVL 58

Expert Comment

by:Gary
ID: 40223144
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 61

Expert Comment

by:gheist
ID: 40223151
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
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 40225579
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
 
LVL 61

Expert Comment

by:gheist
ID: 40225614
so article.php?id="number_of_article" is your permalink
you cannot change anything.
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 40225624
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
 
LVL 58

Accepted Solution

by:
Gary earned 440 total points
ID: 40225631
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
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 40225656
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
 
LVL 58

Assisted Solution

by:Gary
Gary earned 440 total points
ID: 40225663
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
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 40225667
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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now