Forum Moderators: phranque

Message Too Old, No Replies

rewriting part of my php application to create frendly urls

         

zoreli

6:07 pm on May 23, 2012 (gmt 0)

10+ Year Member



Hi to all

I need your help to create friendly url's for a website.

My links are as follow:

www.mysite.com/articles_en.php?artid=89, where I will have to change the url in this:

www.mysite.com/articleTitle

Then I have this url:

www.mysite.com/halls.php?fairid=65 which should become

www.mysite.com/fairname

And www.mysite.com/companies.php?fairid=65&hallid=23 which should become

www.mysite.com/fairname/hallname

You get the idea.

Now...I create a table in the database named seourls in which i have 2 fields only, that is path and url, where the path is mykeyword-secondkeyword and the url is mypage.php?id=1


path = Add-Your-Company
url = articles_en.php?artid=89


I create a script nameed url_rewrite.php

Here is the code:

function curPageURL() {
$url = $_SERVER["REQUEST_URI"];
$path=mysql_result(mysql_query("SELECT path FROM seourls WHERE url='$url'"),0,"path");
return $path
}

curPageURL();


I have the following .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) /url_rewrite.php?path=$1 [L,QSA]

How can I now tell the .htaccess to get the $path from the url_rewrite.php and perform the actual rewriting?

Any help will be deeply appreciated.

Regards,Zoran

g1smd

7:23 pm on May 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You have some of the basics right, but I'm concerned about the words "How can I now tell the .htaccess to get the $path from the url_rewrite.php and perform the actual rewriting?".

The actual rewrite function is right there in the RewriteRule you presented. It translates a friendly URL request to an internal filepath and filename with parameters. That is what is then presented to the internal script for processing. You might find things a little easier if the friendly URLs are of the form
example.com/c5-catname/p4-pagename
as the URL will then contain the IDs needed when the PHP script needs to look up the database records in order to serve the page content for this request.

Your script MUST send the correct 404 headers and human-readable error message when there is no database record for the current request.

If you're on about changing the URLs in the links on the pages of your site, mod_rewrite plays no part in that. You need to alter the PHP code to produce links that look like
href="/friendly-url"
in place of the old
href="index.php?param=value"
format.

You'll also need a redirect such that when the
example.com/index.php?param=value"
format is requested, the user is told to make a new request for the
example.com/friendly-url"
format. For that, you internally rewrite those requests to a special PHP script that looks up the new URL in the database and then uses the PHP HEADER directive to send the correct 301 redirect response. In this case, mod_rewrite does not send the redirect response.


Which part of the process is the code you presented above trying to do? Is it the redirect or the rewrite? The confusing thing is that both actions can each be coded using a RewriteRule. I suspect that it's actually the redirect and in that case the special PHP script should send the 301 redirect header pointing to the new URL when the parameterised URL is requested.

zoreli

2:33 pm on May 24, 2012 (gmt 0)

10+ Year Member



Hi g1smd, thanks for your answer.

I make small progress from yesterday, and here is my current code, which however, still doesn't work , and I have no clue why.

Database table is as follow:


--
-- Table structure for table `seourls`
--

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');



My php code in the file named url_rewrite.php is as follow:


function get_path() {

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

$add="http://www.mysite.com/temporaryfolder/";
$path = $add.''.$startpath;
header("Location: $path");
}


I include this file in my header like this:


include 'url_rewrite.php';
get_path();


Finally my .htaccess is as follow:


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) /url_rewrite.php?path=$1 [L,QSA]


My problems are the following:

When I type for example :mysite.com/temporaryfolder/articles_en.php?artid=89 I get 404 error (page can not be found)

Second problem is what should I do if the query within the url_rewrite.php return no results?

Any help? I am on right track? Any possible solution?

Regards,Zoreli

g1smd

9:07 pm on May 24, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Does this give you any ideas?
[webmasterworld.com...]