nicholasstephan Posted September 30, 2009 Share Posted September 30, 2009 So - The goal is pretty simple, but the application is proving harder than I thought it would be. I'd like to let people type in (and see) friendly looking URLs, but have them load dynamic content. For example: instead of http://www.mysite.com/article.php?id=1234?uid=4321 (whatever), I'd like to see http://www.mysite.com/articles/ArticleTitle. To do this, I'm thinking: 1) with .htaccess, I shunt Apache 404 errors to a page that uses Php's $_SERVER global to grab the url and split it up into variables. 2) I use those variables to run MySQL queries, read files, build and populate pages, etc... 3) and output the results. If the exact article is found, display it. If it's not, be able to say "did you mean... " and treat it like a search. This seems to be all well and good, but then you try to click on something and you find that because you haven't redirected the user, all your relative links are relative to a non-existing directory! :'( So href="about.php" goes to "whatever_the_user_typed/about.php"... not exactly a good situation, considering this just leads back to the 404 page and everything gets very confused. I can put a redirect in there, so the user types /team/john and gets forwarded onto /team.php?id=123, but that kind of defeats the purpose as you want them to be able to look up and see something they might remember and tell others. I've looked into redirecting, then using a apache mod_rewrite, but it seems stupid redirect (changing the url), then manually change it back again... :-\ Am I going about this entirely the wrong way. There must be a solution out there... Anyone have any advice. I don't need someone to write the code for me, I can muddle through it, just some direction... maybe a couple of links... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/176016-friendly-urls/ Share on other sites More sharing options...
trq Posted September 30, 2009 Share Posted September 30, 2009 Am I going about this entirely the wrong way. Indeed you are. mod_rewrite should be all you need. Quote Link to comment https://forums.phpfreaks.com/topic/176016-friendly-urls/#findComment-927465 Share on other sites More sharing options...
RussellReal Posted September 30, 2009 Share Posted September 30, 2009 to make this more friendly you'd probably want to do something like: RewriteRule ^articles/(.*?)/.*$ article.php?id=$1 that will turn a url like: http://www.mysite.com/articles/1234/COOL_WAY_TO_DO_STUFF into http://www.mysite.com/article.php?id=1234 transparently.. meaning that url will redirect you to the second url without ACTUALLY redirecting, just showing the content from the second url meanwhile keeping the first url, but this new url rewriting system MIGHT mess up certain things.. such as images and links. be wary Quote Link to comment https://forums.phpfreaks.com/topic/176016-friendly-urls/#findComment-927468 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.