1,105,556 Community Members

URL rewriting woes

Member Avatar
dyingatmidnight
Junior Poster
153 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
blocblue
Practically a Posting Shark
828 posts since Jan 2008
Reputation Points: 238 [?]
Q&As Helped to Solve: 162 [?]
Skill Endorsements: 16 [?]
Featured
 
0
 

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

Member Avatar
dyingatmidnight
Junior Poster
153 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
veedeoo
Veteran Poster
1,017 posts since Oct 2011
Reputation Points: 314 [?]
Q&As Helped to Solve: 166 [?]
Skill Endorsements: 17 [?]
 
0
 

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.

Member Avatar
diafol
Where are my eyes?
13,004 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,850 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

Rewrites should be placed in .htaccess :

Like veedeoo says.

RewriteEngine On
RewriteRule ^([^/]*)/$ /index.php?id=$1 [L]

Would change http://www.example.com/index.php?id=aboutus to http://www.example.com/aboutus/

etc

Member Avatar
dyingatmidnight
Junior Poster
153 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
diafol
Where are my eyes?
13,004 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,850 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

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.

Member Avatar
blocblue
Practically a Posting Shark
828 posts since Jan 2008
Reputation Points: 238 [?]
Q&As Helped to Solve: 162 [?]
Skill Endorsements: 16 [?]
Featured
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article