spacepoet Posted February 14, 2011 Share Posted February 14, 2011 I have a section on my admin area (a TEXT field) where I want people to be able to give a page a name like: /My-Web-Page/ So it displays on the frontend like: http://www.MyWebsite.com/My-Web-Page/ Instead of: http://www.MyWebsite.com/myPage.php?id-1 Is this something I can do on the frontend with code? Somehow check for the id, and replace it with the Page name from the admin? I want to make it all dynamic, and haven't done this before. Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/227592-how-do-i-do-url-rewrites/ Share on other sites More sharing options...
requinix Posted February 14, 2011 Share Posted February 14, 2011 1. Store the ID/URL pairs in a database. 2. Whenever you try to link to a page, check if there's a corresponding ID in the database (and if so, use the URL instead). 3. Use mod_rewrite on non-existing URLs to send it to a rerouting page. The page looks up the URL in the database: if it finds something it sets up $_GET appropriately and includes myPage.php. (Otherwise it does a 404-type action, like redirecting to the home page or showing an error message.) Quote Link to comment https://forums.phpfreaks.com/topic/227592-how-do-i-do-url-rewrites/#findComment-1173907 Share on other sites More sharing options...
spacepoet Posted February 14, 2011 Author Share Posted February 14, 2011 Hi: Thanks, is there an example on here somewhere .. or a tutorial you know of? I'll see what GOOGLE has to say about it. Quote Link to comment https://forums.phpfreaks.com/topic/227592-how-do-i-do-url-rewrites/#findComment-1174086 Share on other sites More sharing options...
spacepoet Posted February 14, 2011 Author Share Posted February 14, 2011 Maybe I don't understand this, but I'm on GoDaddy and can't get this to work. This is the .htaccess file: Options +FollowSymLinks RewriteEngine on RewriteOptions Inherit RewriteRule ^Delaware-County-General-Contractor([^-]*)\.php$ /myDisplayData.php?id=$1 [L] What I am trying to do is replace the "myDisplayData.php?id=1" extension with "Delaware-County-General-Contractor.php", not send it from "myDisplayData.php?id=1" to "Delaware-County-General-Contractor.php" Maybe I am not understanding how this works, or can this even be done? Quote Link to comment https://forums.phpfreaks.com/topic/227592-how-do-i-do-url-rewrites/#findComment-1174174 Share on other sites More sharing options...
requinix Posted February 14, 2011 Share Posted February 14, 2011 With that RewriteRule the URL has to look like /Delaware-County-General-Contractor1.php So I'll double-check this: 1. You want people to type in "/Delaware-County-General-Contractor.php" and pretend like it's myDisplayData.php?id=1. 2. You want someone to be able to change those at will without having to (for example) change the .htaccess file? Quote Link to comment https://forums.phpfreaks.com/topic/227592-how-do-i-do-url-rewrites/#findComment-1174184 Share on other sites More sharing options...
spacepoet Posted February 14, 2011 Author Share Posted February 14, 2011 Hi: Thanks for getting back to me. I made that with an online mod_rewrite program, so maybe it's not correct? I haven't done this before and with all the overload on GOOGLE I'm not sure what is correct. I'll explain: I have all the pages in the admin created dynamically and it's working fine. They are TEXTAREAs that allow a user to enter META tag data, page headers, and the page content. I want to add a new field called "Page Name" (like "Delaware-County-General-Contractor.php") that will allow the user to define what they want the page name to be (for SEO purposes) on the frontend. On the front end it currently displays "About Us" as "myDisplayData.php?id=1" What I had hoped to do was figure out a way to dynamically replace - on the frontend only - "myDisplayData.php?id=1" with "Delaware-County-General-Contractor.php" Does this make sense? Is it even possible? Quote Link to comment https://forums.phpfreaks.com/topic/227592-how-do-i-do-url-rewrites/#findComment-1174193 Share on other sites More sharing options...
spacepoet Posted February 15, 2011 Author Share Posted February 15, 2011 Hi there: OK, I have it working. I had it backwards! That's why it was not working. I thought the link was suppose to have the QueryString like: <a href="myDisplayData.php?id=1">Link</a> I now see it needs to be: <a href="Delaware.County.General.Contractor.php">Link</a> From my .htaccess file: Options +FollowSymLinks RewriteEngine on RewriteOptions Inherit RewriteRule ^Delaware.County.General.Contractor([^-]*)\.php$ /myDisplayData.php?id=$1 [L] This is very nice. So, am I able to make the .htaccess file dynamic and pull data into it from the database? It seems odd to have to hardcode it everytime. Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/227592-how-do-i-do-url-rewrites/#findComment-1174319 Share on other sites More sharing options...
spacepoet Posted February 15, 2011 Author Share Posted February 15, 2011 Or, maybe I'm not following this ... Doesn't seem to be working ... .htaccess Options +FollowSymLinks RewriteEngine on RewriteOptions Inherit RewriteRule ^Delaware-County-General-Contractor([^-]*)\.php$ /myDisplayData.php?id=$1 Link: <a href="Delaware-County-General-Contractor.php">TEST</a> Error: Could not get data from db: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 I know the page works fine if I type-in "myDisplayData.php?id=1" - I'm looking at it now - looks perfect. What am I missing?? Also, how do I set it so a domain name (site default) will be redirected to "myDisplayData.php?id=1" ? Like if someone types in "www.MyWebsite.com" it goes to the homepage >> "myDisplayData.php?id=1" Quote Link to comment https://forums.phpfreaks.com/topic/227592-how-do-i-do-url-rewrites/#findComment-1174323 Share on other sites More sharing options...
requinix Posted February 15, 2011 Share Posted February 15, 2011 Like I said, with the Rule you have now you have to put the number in the URL. For testing purposes, RewriteRule ^Delaware-County-General-Contractor\.php$ /myDisplayData.php?id=1 However, as you've realized, using a bunch of those rules means editing the .htaccess. Something which you should not need to do. So here's the first part: making mod_rewrite dynamic. RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ redirect.php [L] The second part is writing redirect.php. If it looks at $_SERVER["REQUEST_URI"] it can figure out whether the link exists or not. $url = strtok(substr($_SERVER["REQUEST_URI"], 1), "?"); // eg, $url = "Delaware-County-General-Contractor.php" // now look up in your database which ID corresponds to that $url // if you find a match { // $id = the ID coming from the database $_GET["id"] = $id; include "myDisplayData.php"; // } else { header("HTTP/1.1 404 Not Found"); // show a proper 404 page (however you do that) // } As for the rest: - making your website use Delaware-County-General-Contractor instead of the myDisplayData.php link - setting a default page it depends on the rest of the site. Like on the software it uses and how it's set up. Quote Link to comment https://forums.phpfreaks.com/topic/227592-how-do-i-do-url-rewrites/#findComment-1174337 Share on other sites More sharing options...
spacepoet Posted February 15, 2011 Author Share Posted February 15, 2011 Hi: Thanks for showing me this - maybe I'm still not getting how all of this is suppose to come together .. I made the .htaccess like you posted (copied and pasted it) I made the redirect.php like this: <?php //include('include/myConn.php'); // Do I need to include a DB connection? $myURL = strtok(substr($_SERVER["REQUEST_URI"], 1), "?"); // eg, $myURL = "Delaware-County-General-Contractor.php" // now look up in your database which ID corresponds to that $myURL // if you find a match { // $id = the ID coming from the database $_GET["id"] = $id; include "myDisplayData.php"; // } else { header("HTTP/1.1 404 Not Found"); // show a proper 404 page (however you do that) // } ?> I added a new field in my database called "myURL" Now, I am opening the page in a browser like this: http://www.website.com/myDisplayData.php?id=1 It displays fine, and I added several test links: HARDCODED: <a href="Delaware-County-General-Contractor">Delaware-County-General-Contractor</a> FROM DB USING THE MYURL FIELD: <a href="Delaware-County-General-Contractor.php">Delaware-County-General-Contractor.php</a> But when I click either links I get the error: Could not get data from db: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Do I need to somehow include the "redirect.php" in the "myDisplayData.php" page ?? Or, am I completely off base with this ?? I asked GODaddy, and they to me the mod-rewrite was turned on ... ?? Thanks for taking the time to work with me on this - I appreciate it! Quote Link to comment https://forums.phpfreaks.com/topic/227592-how-do-i-do-url-rewrites/#findComment-1174396 Share on other sites More sharing options...
requinix Posted February 15, 2011 Share Posted February 15, 2011 What's in your .htaccess and what's in your redirect.php? Literally. Quote Link to comment https://forums.phpfreaks.com/topic/227592-how-do-i-do-url-rewrites/#findComment-1174417 Share on other sites More sharing options...
spacepoet Posted February 15, 2011 Author Share Posted February 15, 2011 .htaccess RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ redirect.php [L] redirect.php <?php //include('include/myConn.php'); // Do I need to include a DB connection? $myURL = strtok(substr($_SERVER["REQUEST_URI"], 1), "?"); // eg, $myURL = "Delaware-County-General-Contractor.php" // now look up in your database which ID corresponds to that $myURL // if you find a match { // $id = the ID coming from the database $_GET["id"] = $id; include "myDisplayData.php"; // } else { header("HTTP/1.1 404 Not Found"); // show a proper 404 page (however you do that) // } ?> Just like that. What did I miss ?? Quote Link to comment https://forums.phpfreaks.com/topic/227592-how-do-i-do-url-rewrites/#findComment-1174422 Share on other sites More sharing options...
requinix Posted February 15, 2011 Share Posted February 15, 2011 You're... supposed to fill in the bits of redirect.php that I didn't write. Like, most of it. I have no idea what kind of code you have - there's no way I could do it for you. Read the comments and add the necessary code. Quote Link to comment https://forums.phpfreaks.com/topic/227592-how-do-i-do-url-rewrites/#findComment-1174425 Share on other sites More sharing options...
spacepoet Posted February 15, 2011 Author Share Posted February 15, 2011 Ohhhhh ... I got ya .. I have never done this so I'm flying blind on this .. You mean I need to somehow pull the data out of the DB, and match it up, right? Like: <?php include('include/myConn.php'); // Do I need to include a DB connection? $myURL = strtok(substr($_SERVER["REQUEST_URI"], 1), "?"); $query = mysql_query("SELECT id,myURL FROM myWebSiteData"); while($redirectData = mysql_fetch_array($query)) { echo "$myURL = ". $redirectData['$myURL'] .""; } // now look up in your database which ID corresponds to that $myURL // if you find a match if //{ echo \"$id = \". $redirectData['$id'] .\"\"; $_GET["id"] = $id; include "myDisplayData.php"; } else { header("HTTP/1.1 404 Not Found"); // show a proper 404 page (however you do that) } ?> Which I know is not correct but I can't keep my eyes focused, anymore ...lol .. Am I on the right track? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/227592-how-do-i-do-url-rewrites/#findComment-1174433 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.