doubledee Posted August 7, 2011 Share Posted August 7, 2011 I am re-writing my website from physical files and directories to using MySQL and making things more "virtual". Could definitely use some help making the switch! What I currently have... (All physical files and directories) www . MyWebsite .com/index.php www. MyWebsite .com/articles/how-do-i-incorporate.php www. MyWebsite .com/articles/why-you-need-a-postage-meter.php www. MyWebsite .com/articles/contractors-trouble-or-treasure.php I am going to put those articles in MySQL, and so I'd likely have articleID=1 articleID=2 articleID=3 Questions: 1.) How should my new, dynamic URL look (before I use Mod_Rewrite)? www . MyWebsite .com?id=1234 www . MyWebsite .com/index.php?id=1234 www . MyWebsite .com/page.php?id=1234 www . MyWebsite .com/articles/index.php?id=1234 www . MyWebsite .com/articles/page.php?id=1234 www . MyWebsite .com/articles/article.php?id=1234 www . MyWebsite .com/articles/article.php?articleID=1234 other? 2.) Should my database have an "articleID"? 3.) Can I instead use a name - fashioned for my URL - as the primary key? For example: "is-the-chamber-of-commerce-for-me" 4.) How do I go from having articleID=1234 to "radio-spots-are-within-your-reach.php" ? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/ Share on other sites More sharing options...
trq Posted August 7, 2011 Share Posted August 7, 2011 1) As I said earlier, you will need to pass the title of the article not the id. 2) Yes. Each row should have a unique primary key. 3) Yes. 4) Have you looked at any mod_rewrite tutorials. this is a pretty straight forward rule and would be covered in any beginner tutorial. Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1253905 Share on other sites More sharing options...
doubledee Posted August 8, 2011 Author Share Posted August 8, 2011 1) As I said earlier, you will need to pass the title of the article not the id. 4) Have you looked at any mod_rewrite tutorials. this is a pretty straight forward rule and would be covered in any beginner tutorial. There seems to be a big disconnect here... Currently, I have an article summary page ("www .MySite.com/article_index.com/articles/a_index.php"). In the past each article summary pointed to a physical file (e.g. "www .MySite.com/articles/deducting-your-home-office.php") Since I will no longer have physical articles, I moved my article summary page to the web root (i.e. "www .MySite.com/a_index.php") And for each summary, I changed my hyperlink to something like this... <a href="<?php echo WEB_ROOT ?>article.php?id=1"> which gives me a URL like... "www .MySite.com/article.php?id=1" Why do you say to not use an "id"?? Isn't that how you refer to things in a database? I thought I referred to my new dynamic article like the link above and then Mod_Rewrite changed it to a "pretty" URL like this... "www .MySite.com/articles/deducting-your-home-office.php" Help me figure this out... Also, what about my "articles" directory?! Since I no longer will have hundreds and thousands of article files, I guess I don't need the directory, HOWEVER, I still want it in the URL so people know they are reading an "article" ("/articles/how-much-insurance-is-enough.php") Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1253931 Share on other sites More sharing options...
trq Posted August 8, 2011 Share Posted August 8, 2011 The reason you can;t use an id is because there is no way of relating a url like /this-is-article-foo.php with and id. Instead of passing an id you would need to pass the title, making your underlying urls look something like: /article.php?title=this-is-article-foo You can then search the database by title, not id. And indeed, you no longer need your articles directory. You simply use mod-rewrite to make it look like it still exists. the first thing you need to do is get this all in place and working without mod_rewrite. You can then worry about adding your rules to make it look pretty. Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1253944 Share on other sites More sharing options...
doubledee Posted August 8, 2011 Author Share Posted August 8, 2011 The reason you can;t use an id is because there is no way of relating a url like /this-is-article-foo.php with and id. I'm dying here (and totally not understanding any of this)... I thought you create a URL that works and then you use Mod_Rewrite to make it pretty? If I have an "article record" which contains an "id" field and a "urlTitle" field, then why can't I switch between the two? Others have told me to use an ID and then make it pretty like I want, but you are saying that is impossible? If I start with a pretty URL like "www .MySite.com/creating-tableless-page-layouts.php" then why would I need to use mod_rewrite?! And it also seems like a bad idea having a primary key "creating-tableless-page-layouts"?! Instead of passing an id you would need to pass the title, making your underlying urls look something like: /article.php?title=this-is-article-foo You can then search the database by title, not id. And indeed, you no longer need your articles directory. You simply use mod-rewrite to make it look like it still exists. the first thing you need to do is get this all in place and working without mod_rewrite. You can then worry about adding your rules to make it look pretty. Well, I have it working using "id" but you are saying not to do that?! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1253964 Share on other sites More sharing options...
trq Posted August 8, 2011 Share Posted August 8, 2011 You really are missing massive chunks here. I thought you create a URL that works and then you use Mod_Rewrite to make it pretty? You do. That is exactly what I said to do. If I start with a pretty URL like "www .MySite.com/creating-tableless-page-layouts.php" then why would I need to use mod_rewrite?! I didn't say you would start with a url like that. I said you would start with a url like: /article.php?title=this-is-article-foo Why? Because all mod-rewrite does is matches a pattern in a string and if found, will pass that string to your underlying php page. This means if your script is expecting an id, then your url rule will need to pass that to the script. Meaning you will never be able to use urls such as /creating-tableless-page-layouts.php, you rould need to use something more like /245.php And it also seems like a bad idea having a primary key "creating-tableless-page-layouts"?! I'm really not sure where you get this stuff from. Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1254006 Share on other sites More sharing options...
trq Posted August 8, 2011 Share Posted August 8, 2011 It'll probably become clearer once you have read a few mod-rewrite tutorials. if you have a working url now (even using an id) maybe you should read a few of those now. Once you understand how it all works you can then go and change your script to use title or some other friendlier way of being identified. Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1254008 Share on other sites More sharing options...
doubledee Posted August 8, 2011 Author Share Posted August 8, 2011 You really are missing massive chunks here. That's why you are an Administrator and I'm a Newbie... If I start with a pretty URL like "www .MySite.com/creating-tableless-page-layouts.php" then why would I need to use mod_rewrite?! I didn't say you would start with a url like that. I said you would start with a url like: /article.php?title=this-is-article-foo Why? Because all mod-rewrite does is matches a pattern in a string and if found, will pass that string to your underlying php page. This means if your script is expecting an id, then your url rule will need to pass that to the script. Meaning you will never be able to use urls such as /creating-tableless-page-layouts.php, you rould need to use something more like /245.php I thought I was doing what you said above, but it doesn't work?! article_index.php <a href="<?php echo WEB_ROOT ?>article.php?url_title=all-about-creating-dynamic-content"> article.php // Build query. $q = 'SELECT meta_description, meta_keywords, title, subtitle, written_on, author, body, end_notes FROM article WHERE url_title=?'; // WHERE id=?'; // Prepare statement. $stmt = mysqli_prepare($dbc, $q); // Bind variable. mysqli_stmt_bind_param($stmt, 's', $urlTitle); //mysqli_stmt_bind_param($stmt, 'i', $id); // Execute query. mysqli_stmt_execute($stmt); // Transfer result-set from prepared statement. // (Must call for every query that successfully produces a result set.) mysqli_stmt_store_result($stmt); // Check for Article Record. if (mysqli_stmt_num_rows($stmt)==1){ // Article was Found. $articleExists = TRUE; // Bind result variables. mysqli_stmt_bind_result($stmt, $metaDescription, $metaKeywords, $title, $subtitle, $writtenOn, $author, $body, $endNotes); // Fetch record. mysqli_stmt_fetch($stmt); } ?> <?php if ($articleExists){ echo '<h1>' . $title . '</h1>'; echo '<p class="articleDate">Published: ' . $writtenOn . '</p>'; echo $body; echo '<hr></hr> <div id="endnotes"> <h2>Endnotes:</h2>' . $endNotes . '</div>'; }else{ // Update failed. echo '<h1>No Such Article</h1>'; echo '<p>Sorry! The article you were looking for could not be found.</p>'; ?> My old code using "id" was working. This code with "urlTitle" does not return the article. And it also seems like a bad idea having a primary key "creating-tableless-page-layouts"?! I'm really not sure where you get this stuff from. You're kidding, right?! (I don't know a DBA in the world that would use "creating-tableless-page-layouts" as a primary key. Not even an MS Access developer.) Debbie Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1254023 Share on other sites More sharing options...
trq Posted August 8, 2011 Share Posted August 8, 2011 Do the titles look like this in your database? all-about-creating-dynamic-content You will likely need to strip the - either that or what most people do is actually have a separate slug field and use that instead. And what I meant by "I'm really not sure where you get this stuff from." is that I never told you to make your title your primary key. You just seem to read things that aren't there. Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1254025 Share on other sites More sharing options...
doubledee Posted August 8, 2011 Author Share Posted August 8, 2011 It'll probably become clearer once you have read a few mod-rewrite tutorials. if you have a working url now (even using an id) maybe you should read a few of those now. Once you understand how it all works you can then go and change your script to use title or some other friendlier way of being identified. I'm having a bad (and very unproductive) day. On Friday people were pushing me to use an ID, but I wanted a pretty URL. I figured I could use an ID to look up the article and then display the corresponding article title since they are all stored in the database. (I did find something on Google about a "re-write look-up map" thingy which would take an ID and map it to "all-about-dynamic-pages".) After reading your last post, I thought that if I used: www. MyWebsite.com/article.php?url_title=all-about-creating-dynamic-content That would find the article in the database and then I could use mod_rewrite to produce... www. MyWebsite.com/articles/all-about-creating-dynamic-content Debbie Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1254026 Share on other sites More sharing options...
trq Posted August 8, 2011 Share Posted August 8, 2011 You obviously need to eventually pass something to your script that it can use in a query to identify a particular article. Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1254027 Share on other sites More sharing options...
doubledee Posted August 8, 2011 Author Share Posted August 8, 2011 Do the titles look like this in your database? all-about-creating-dynamic-content Because I wasn't sure how this would all end up, I created a record that covers my bases: id url_title title 2 all-about-creating-dynamic-content All About Creating Dynamic Content You will likely need to strip the - either that or what most people do is actually have a separate slug field and use that instead. I think that's what I did above? And what I meant by "I'm really not sure where you get this stuff from." is that I never told you to make your title your primary key. You just seem to read things that aren't there. You said, "Instead of passing an id you would need to pass the title, making your underlying urls look something like: /article.php?title=this-is-article-foo" I guess I read too much into that and thought you meant make "title" a primary key. I guess you just meant query on it because we are both assuming that "Title" is unique - like a PK - right? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1254030 Share on other sites More sharing options...
trq Posted August 8, 2011 Share Posted August 8, 2011 Yeah, your title would need to be unique and what you have done is a great idea. It's the same idea as the slug I linked to earlier. So, is it working now? Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1254035 Share on other sites More sharing options...
doubledee Posted August 8, 2011 Author Share Posted August 8, 2011 Yeah, your title would need to be unique and what you have done is a great idea. It's the same idea as the slug I linked to earlier. I did that this morning before I even knew what a "slug" was?! So do my code snippets look okay? Is that what you have been trying to tell me all night? So, is it working now? Nope, it goes to the ELSE branch and echos "Sorry! The article you were looking for could not be found." Here is what I'm doing (again)... 1.) I launch: http://localhost/01_MyProject/article_index.php 2.) I click on: <a href="<?php echo WEB_ROOT ?>article.php?url_title=all-about-creating-dynamic-content"> 3.) I go to the page: http://localhost/01_MyProject/article.php?url_title=all-about-creating-dynamic-content Debbie Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1254038 Share on other sites More sharing options...
doubledee Posted August 8, 2011 Author Share Posted August 8, 2011 Thorpe, In my exhaustion, looks like I left out an important part of code... if (isset($_GET['url_title'])){ // Title exists. $urlTitle = $_GET['url_title']; It is working now. Well, at least the ugly URL. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1254046 Share on other sites More sharing options...
trq Posted August 8, 2011 Share Posted August 8, 2011 That will happen to the best of them on occasions. Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1254053 Share on other sites More sharing options...
doubledee Posted August 8, 2011 Author Share Posted August 8, 2011 That will happen to the best of them on occasions. And especially a wimp like me!!! So, can you please give me a bump here? When I click on an article in my Article Index page, I get something like this... http://localhost/01_MyProject/article.php?url_title=all-about-creating-dynamic-content I would like to get back to what I had before with physical files like this... http://localhost/01_MyProject/articles/all-about-creating-dynamic-content I have been genuinely Googling, but I'm tired and can't find an example of exactly what I need. Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1254057 Share on other sites More sharing options...
doubledee Posted August 8, 2011 Author Share Posted August 8, 2011 Here is my best guess at things, but it doesn't work?! RewriteEngine on RewriteRule ^articles/([^/\.]+)/?$ /article.php?url_title=$1 [L] Debbie Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1254066 Share on other sites More sharing options...
cags Posted August 8, 2011 Share Posted August 8, 2011 Define doesn't work. That looks more of less correct. With that in place you should be able to take one of your 'pretty' urls, paste it in your address bar and view the correct page. Once that works the final stage is to change how you output the links on your page to match the pretty version. Et Voila, jobs a good un'. Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1254481 Share on other sites More sharing options...
doubledee Posted August 9, 2011 Author Share Posted August 9, 2011 Define doesn't work. That looks more of less correct. With that in place you should be able to take one of your 'pretty' urls, paste it in your address bar and view the correct page. Once that works the final stage is to change how you output the links on your page to match the pretty version. Et Voila, jobs a good un'. Like I said before, it doesn't work... Can I please get some help understanding how to write a proper mod_rewrite for what I want to do? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1254668 Share on other sites More sharing options...
trq Posted August 9, 2011 Share Posted August 9, 2011 The forward slash here: /article.php?url_title=$1 makes the rule direct to a file article.php within your web servers document root. You appear however to be running this within a sub directory (01_MyProject). Try using a relative path. Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1254740 Share on other sites More sharing options...
cags Posted August 9, 2011 Share Posted August 9, 2011 Saying it doesn't work, without any more information is about as useful as going to a doctors and saying you are unwell. They know that, that's why you are there, without a description of the symptoms you are never going to receive an informed answer. Sensible responses would have been along the lines of... "I get a 404 page from my browser" "I get a 500 page from my browser" "I see a blank page" "The URL in the address bar changes, but it shows http://www.badgersftw.com, any idea why?" If you don't help us help you, then we either won't be able to help, or just plain won't bother. Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1254750 Share on other sites More sharing options...
doubledee Posted August 10, 2011 Author Share Posted August 10, 2011 I have spent A LOT of time doing my homework and reading up on mod_rewrites, but I still need some help. Here is my file... #PRETTY: articles/postage-meters-can-save-you-money #UGLY: article.php?title=postage-meters-can-save-you-money RewriteRule articles/([a-zA-Z0-9_-]+)$ article.php?title=$1 The rewrite sorta works, however, my includes (e.g. Header, Footer, Right Panel) are *not* working. So apparently I am not setting the PRETTY or UGLY path correctly?! First off, how does my Mod_Rewrite look otherwise?? Secondly, what needs to be fixed to address this issue? (And why exactly isn't it working?!) Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1255138 Share on other sites More sharing options...
trq Posted August 10, 2011 Share Posted August 10, 2011 First off, how does my Mod_Rewrite look otherwise?? Yeah, looks good. Secondly, what needs to be fixed to address this issue? (And why exactly isn't it working?!) Are you using urls to include files? You shouldn't be. Post some relevant code. Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1255151 Share on other sites More sharing options...
cags Posted August 10, 2011 Share Posted August 10, 2011 As thorpe says, your includes should be a relative file path to where the actual served file is. So since your ugly path is article.php, the include statement within should be a relative file path to that. Assuming article was in root for example and you have a header.php in an includes folder... include 'includes/header.php'; This is not to be confused with the path for css/js/image files though. Since these are requested from the clients browser, which have no idea of the file structure, any src attribute in your file should be a URI path relative to either root (simplest), or the pretty url (get's difficult if you wish to include a css file in your header.php for example, because you could be using your header.php file from different URI 'depths'). <!-- relative to your root directory --> <script type="text/javascript" src="/assets/js/jquery.js" /> <!-- full path --> <script type="http://www.example.com/assets/js/jquery.js" /> These both obviously assume that you have an assets folder in your DOCUMENT_ROOT folder. Quote Link to comment https://forums.phpfreaks.com/topic/244133-need-help-switching-from-physical-to-virtual-set-up/#findComment-1255170 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.