Jump to content

Redirect to directory index.php


doubledee

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/265135-redirect-to-directory-indexphp/
Share on other sites

// 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.

// 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

 

 

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

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.