doubledee Posted July 3, 2012 Share Posted July 3, 2012 I have an "article" directory with two files: "article.php" and "index.php" If "article.php" is called but there is no slug in the URL, I would like Apache to kick in and re-direct the user to "index.php" which is a Listing of Articles. I had this working before, but apparently messed up my .htaccess file. Here is a code snippet to provide some context... // ****************************** // Attempt to Retrieve Article. * // ****************************** if (isset($_GET['slug']) && $_GET['slug']){ // Slug found in URL. }else{ // Slug Not found in URL. // This will never fire!! // Apache catches missing slug and re-routes to "articles/index.php" }//End of ATTEMPT TO RETRIEVE ARTICLE ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> What code do I need to put in my .htaccess to make this happen? Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/265135-redirect-to-directory-indexphp/ Share on other sites More sharing options...
requinix Posted July 3, 2012 Share Posted July 3, 2012 // This will never fire!! If the code is as simple as you posted then yes that most certainly will fire: when there is no "slug" in the URL. On that note, there's no need to get Apache involved: your article.php can very easily redirect, as you apparently had once before. Quote Link to comment https://forums.phpfreaks.com/topic/265135-redirect-to-directory-indexphp/#findComment-1358740 Share on other sites More sharing options...
doubledee Posted July 3, 2012 Author Share Posted July 3, 2012 // This will never fire!! If the code is as simple as you posted then yes that most certainly will fire: when there is no "slug" in the URL. My file is much more complicated than that, but I posted the code that is relevant. And I know for a fact whatever code I had in my .htaccess did a redirect for me, because that is why I added the comments above. (Originally I had error-handling code that was supposed to fire, but it never did because of some mod_rewrite, so I took that erro-handling code out and added the above comments.) On that note, there's no need to get Apache involved: your article.php can very easily redirect, as you apparently had once before. Yes, I could do it in PHP, but it is very common to have Apache redirect to an index.php file when no file exists or like I am describing. I would like to restore things back to what I had... Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/265135-redirect-to-directory-indexphp/#findComment-1358743 Share on other sites More sharing options...
doubledee Posted July 3, 2012 Author Share Posted July 3, 2012 Requinix, Okay, so I tried adding this code... }else{ // Slug Not found in URL. // This will never fire!! // Apache catches missing slug and re-routes to "articles/index.php" //NEW // Redirect to Display Outcome. header("Location: " . BASE_URL . "/articles/index.php"); // End script. exit(); }//End of ATTEMPT TO RETRIEVE ARTICLE ...and it *appears* to do what I need. Any comments? And just for my own edification, can someone show me how to accomplish the same end effect using a mod_rewrite and redirecting to the default directory file which is "/articles/indoex.php" in this case? Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/265135-redirect-to-directory-indexphp/#findComment-1358744 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.