Hi everyone

What I am trying to do:

I am trying to rewrite my urls in seo frendly url. For that purpose I create the following table in my database:

CREATE TABLE `seourls` (
`path` varchar(255) NOT NULL,
`url` varchar(255) NOT NULL,
PRIMARY KEY (`path`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table `seourls`
-- 

INSERT INTO `seourls` (`path`, `url`) VALUES ('building-and-construction', 'halls_en.php?fairid=64'),
('online-fair-platform', 'articles.php?artid=89'),
('add-your-company', 'articles_en.php?artid=89'),
('rough-construction', 'companies_en.php?fairid=64&ehallid=118');

Now... I create a php file, named url_rewrite.php Here is the code that I have there:

         function get_path() {   

         $url1 = $_SERVER["REQUEST_URI"];
         $url = str_replace('/tempfolder/', '', $url1); 
         $startpath=mysql_result(mysql_query("SELECT path FROM seourls WHERE url='$url'"),0,"path");

         //$add="http://www.mydomain.com/tempfolder/";
         //$path =  $add.''.$startpath;
         //header("Location: $path");

         header("Location: $startpath");
         }

What is my problem:

I don't know how to proceed further. I know that I have to use .htaccess to rewrite the url stored in $startpath variable, but I don't know to do that. I have very limited knowledge of regular expressions and 0 knowledge of apache.

Anyone can help with the .htaccess code and (if needed) changes of the url_rewrite.php file? My urls must be seo frendly, but I am not sure if the code header("Location: $startpath"); can be considered as seo friendly redirect.

Regards, Zoreli

Hi Squidge

Thanks for your response.

I make an extensive research on many .htaccess tutorials, but i didn't find any specific tutorial that explain or is close to the task that I want to achieve.

Can you please post some example of .htaccess code that will solve my problem?

Regards,Zoreli

RewriteEngine on
RewriteBase /
RewriteRule ^page/([0-9]+)\.php$ index.php?key=$1

Here is start of one that i use, it rewrites any URL that has index.php?key=$ and re assigns to /page/[keyid]

I have not seen url rewrite done via a DB before, but hey i am sure it will go just as well.
(remove comments)
ReWriteEngine on //Start your rewrite module
RewriteBase / //Sets the base, so all files read/written from [site]/

RewriteRule ^online-fair-platform/([0-9]+)$ articles.php?artid=$1

This would rewrite the link
site.something/articles.php?artid=1554
into
site.something/online-fair-platform/1554

Hope that helps

Member Avatar for diafol

I've found this site absolutely brilliant for mod rewrites: http://www.generateit.net/mod-rewrite/

That site gives:

RewriteEngine On
RewriteRule ^online-fair-platform/([^/]*)$ /articles.php?artid=$1 [L]

Hi dlafol

I've found this site absolutely brilliant for mod rewrites: http://www.generateit.net/mod-rewrite/

Tool seems promsing, but I need database driven solution. Thanks anyway for the link, I may use it for simple sites.

Hi Squidge

I have not seen url rewrite done via a DB before, but hey i am sure it will go just as well.
(remove comments)
ReWriteEngine on //Start your rewrite module
RewriteBase / //Sets the base, so all files read/written from [site]/
RewriteRule ^online-fair-platform/([0-9]+)$ articles.php?artid=$1
This would rewrite the link
site.something/articles.php?artid=1554
into
site.something/online-fair-platform/1554

I think that only way this to be done with the database is all requests for the site to be REDIRECTED TO ulr_rewrite.php and from there I shold again redirect the users...but the question is how to do it? And is that seo frendly?

Any idea?

Regards,Zoreli

zoreli if i may, why are you using the DB for this?

i think you maybe over complicating things

Well, there will be way too many pages on the site, so I can't simply add rules in the .htaccess. Sure enough this code will be reused in each and every project after...

You could use the naming style you want as the key indetifier instead of a numerical one. So for example you URL would be somesitename.blah/ulr_rewrite.php?indentifier=online-fair-platform

You will still be required to use .htaccess to rewrite it to somesitename.blah/online-fair-platform
with a rewrite rule something like

RewriteRule ^\$/([^/]*)$ /ulr_rewrite.php?indentifier=$

Please note this is not tested, and may not function, or may even require tweaking :)

Hope that helps

Member Avatar for diafol

Most big sites these days route their requests through the index.php page and just use includes/templates to serve the info requested from the url (mod-rewritten).

Surely all you need is a similar setup with:

a url like: index.php?article=345
being placed into html by php/mysql as /345/online-fair/

If you have the slugified title of the article in the DB already, why not use it to build your url for a link? All your mod-rewrite then has to do is ignore the title and concentrate on the number.

8m month old thread really????

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.