Solved

Rewrite slug

Posted on 2008-06-22
4
277 Views
Last Modified: 2010-03-04
I tried to do this once before and never found a solution that worked so I'm giving it another shot. I know the rewrite rule to rewrite my pages but I'm curious if something like this can be done.

I want to use slugs (keyword) so I can do the following:
www.site.com/keyword

Where keyword would point to:
www.site.com/profiles?lookup=keyword (profile id)

BUT, I would also like to rewrite my other pages as well
www.site.com/whateverpage/

Would actually be
www.site.com/whateverpage.php

Can anyone lend a hand here please?
0
Comment
Question by:SOakley54
  • 3
4 Comments
 
LVL 19

Accepted Solution

by:
BrianGEFF719 earned 500 total points
ID: 21843487
You might consider rewrite all requests to some kind of dispatcher, and then from there you can determine if the keyword is a "reserved" keyword, eg one of your pages such as /contacts or /whateverpage, if not then you can redirect to /profiles?lookup=keyword..

Does that make sense?
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 21843507
You might make a dispatcher that does something like this:

<?php
switch(strtolower($_SERVER['REQUEST_URI']))
{
      case '/':
            require_once 'home.php';
            break;
      case '/contact':
            require_once 'contact.php';
            break;
      case '/aboutus':
            require_once 'aboutus.php';
            break;
      default:
            require_once 'profiles.php'; /* run the profiles script to determine which profile it is */      
}
?>
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 21843556
Okay, so this might work, try this rewrite rule:

RewriteRule ^(.+)/?$      dispatcher.php?path=$1       [L]


Now, our dispatcher.php:

<?php
switch(strtolower($_GET['path']))
{
      case '/':
            require_once 'home.php';
            break;
      case '/contact':
            require_once 'contact.php';
            break;
      case '/aboutus':
            require_once 'aboutus.php';
            break;
      default:
            require_once 'profiles.php'; /* run the profiles script to determine which profile it is */      
}
?>



Now our profiles.php:

<?php
 echo "You're looking for the profile at: {$_GET['path']}";
?>




This is just an idea...I'm eager to what other people think about this.
0
 

Author Comment

by:SOakley54
ID: 21849907
Once I modified my Google search to "URL Routing" I came up with a few better hits. Ideally it would be great to just port all my work over to something like ZF or Cake and use their well written MVC architecture instead of writing my own (non-MVC) but it's just not in the cards at this time.

All of my development on this project is at home so I can't post up what I have so far. To be honest I can't say I truly care for the approach I'm investing in either. To me it's like a really half-assed version of MVC except just the C part; sigh. The good news is that it is routing properly. The bad news is that I have to hold all of my url calls inside of a switch() statement which is quite time consuming to port over right now. Especially because all of my pages have to be self-contained inside of a function with alternate switches to get the parameters.

Like I said, I'll throw the code up when I get home and get some more eyes on this. It would be interesting to see what I get back and thank you for taking the time out as well. It's very much appreciated :)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

777 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