/    Sign up×
Community /Pin to ProfileBookmark

Mapping URL to another URL

Hi all

I would like to map human readable URLs to the PHP generated URLs on my page.

For instance I would like users to be able to type [url]http://www.mysite.com/a-brand-new-article.php[/url] and map the URL to something like [url]http://www.mysite.com/art.php?aid=1[/url]

I currently do this by having a custom 404 script set up in apache. All the human readable requests generate a 404 and the script will query the DB and map the article name to the article ID. Then the script will send a header response and another page will then retrieve the content using the article ID.

This however imposes some overhead (2 request for every page view) as well as filling up the Apache error log with a huge amount of 404 entries.

1: How do I avoid that the user will see [url]http://www.mysite.com/art.php?aid=1[/url] in the address bar. I would of course like the [url]http://www.mysite.com/a-brand-new-article.php[/url] URL to stay.
2: Can I use mod rewrite to do this? (how would you access the DB?)
3: Maybe I need another approach to the solution?

Any help is appreciated.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@auxoneAug 20.2008 — I donno if this helps, but I recently have been trying to write a REST interface for my web server. My approach is slightly different but maybe it will help you.

Firstly, I use a mod_rewrite such as this:
[CODE]RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA][/CODE]


This will could be slightly modified to do what you want. In it's current form it takes example.com/3/2/1 and makes a single $_GET containing 3/2/1. Still, the final RewriteRule isn't really necessary as the following code works just fine.

You can then do something like:

[code=php]$url = substr($_SERVER['REQUEST_URI'], strlen($path)); // path cuts off the directory you are in, if not root
$url_array = explode('/', $url); [/code]


Or in your case maybe explode the - and do some parsing of your own?

(This solutions doesn't change how the URL looks to the user, and the DB portion should just be code you run after parsing the URL)
Copy linkTweet thisAlerts:
@DokauthorAug 21.2008 — But with your approach I have to know the ID of an article in advance right?
×

Success!

Help @Dok spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...