Hello all, so I was tasked with looking into a way to rewrite our dynamic url's to something more user and seo friendly. I have absolutely no exprience in this area so it's been a lot of googling and trial and error. However; I've reached something of a wall in my research.

We're using apache so my research introduced me to mod_rewrite and htaccess files. I then created this little function in the php page that creates a new page - so that it's called whenever a page is created or edited.

function writeRule($dir, $id){
    $text = file("../.htaccess");
    $file = fopen("../.htaccess", 'w') or die(@mysql_error());

    foreach($text as $line)
    {
         if (!strstr($line,$id)) //look for $id in each line
           fputs($file,$line); //place $line back in file 
    }

    $dir = str_replace(' ', '', $dir);
    $dir = str_replace('/', '', $dir);
    $dir = str_replace(',', '', $dir);
    $dir = strtolower($dir);
    $rule = "\nRewriteRule ^".$dir."/?$ index.php?id=".$id." [L]\n";
    fwrite($file, $rule);
    fclose($file);



  }

However; my supervisor then tells me that she would rather not write directiy to the htaccess file AND further research told me that I probably shouldn't be doing it this way anyway. So I was basically back at the beginning, trying to find a way to rewrite the url's of pages - not only of the existing links but also on any new pages created by the client using the CMS later down road.

This led me to rewritemaps but we don't have access to the server config files to that's not really an option.

So I guess my question is what's the best practice for rewriting dynamic urls in an equally dynamic environment where you don't have access to server files?

Recommended Answers

All 7 Replies

Can you provide examples of the URL structure you would like, and what you need that URL rewritten back to?

yeah most of our urls are /index.php?id=# and what I would like to do is depending in the id number value change it to a directory name.

For example I'd like /index.php?id=2 to be acessible via /aboutus instead. Rinse and Repeat for all the other urls.

Hi,

Just a question, you mean your supervisor can't tackle this stuff? I am keeping my fingers crossed from now on. I hope I don't work for someone like that after college, or else I will put her in the recycling bin.. Boy, that was a pretty sick thoughts I have, I am just joking of course..

HINT! Since I don't write codes for advance users... sometimes I do, but on a very rare occasion, when they already put a lot of efforts on the table.

You may want to start something like this

RewriteRule ^aboutus/?$ index.php?id=$1 [L]

**$1 ** is not a php variable as we all understand about it, but it is rather a local variable when placed on the .htaccess. Therefore, the id=1 to id=22 are beautifully catched I assumed.

I think her idea is to make me learn and I am :P Okay so since my id's are numerical I would need to rework that somehow. Or rather rework how the page is rendered (since it queries the database based on the id number in th url) but that's not really a rewrite problem that's a php thing I'll have to rewrite the query. Okay off to test things :D

Member Avatar for diafol

Does you db table have a faux page title, e.g.

id pagename (unique) linklabel
1  home              home
2  about-us          about us
3  contact-us        contact us

$d = mysql_fetch_array($r){
  $link = "<a href="/{$d['pagename']}/">{$d['linklabel']}</a>";
}

This used along with the .htaccess file should now work for all pages added to the DB.

Or, you could add the page ID to your URLs. It's not as pretty, but it is common.

E.g. example.com/2-about-us

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.