Page 1 of 1
Remove File Extention from Filename [Solved] (finally)
Posted: Mon Jul 11, 2005 12:19 pm
by theda
I was curious as to how I would go about forcing anyone viewing content directly (like url.com/page.ext, instead of url.com/file.php?variable=file), to redirect to the 'file.php' page, aka the index page.
I've heard it can be done, and is done in a few CMS scripts, but I've never done it myself. Thanks to all who reply.
Edit: Please scroll to the last post.
using $_SERVER['PHP_SELF']
Posted: Mon Jul 11, 2005 1:25 pm
by mltsy
One way to do it is make page.ext a php file, and put something at the top of it like:
if (basename($_SERVER['PHP_SELF']) != "file.php")
header("Location:
http://www.url.com/index.php");
Where file.php is the file that you want to make sure they are looking at rather than viewing page.ext directly, and index.php is the page you are redirecting them to.
Posted: Mon Jul 11, 2005 1:54 pm
by theda
Interesting. That looks like it would do the job. I'll talk to the person who I was going to help recode their website about this, as they were wondering how to do it (and I was curious myself, as I don't have that on my website yet either.).
I'll try it once. Thanks

<Note: Not solved yet ^_^;>
Posted: Mon Jul 11, 2005 1:55 pm
by theda
Oh yeah, I'm also curious if there's a way around $_Server, because my host has that part shut off I believe.
Edit: Nevermind, I remembered $HTTP_SERVER_VARS is the version I use <_< Outdated POS server ^_^;
Posted: Mon Jul 11, 2005 2:00 pm
by Burrito
I think you mean if they try to hit a page that doesn't exist, you want them redirected to the index page correct?
if that's the case, just changing page.ext to page.php isn't gonna cover all of your needs.
you'll need to change your "custom errors" for 404 errors to open a url with whatever you want.
Posted: Mon Jul 11, 2005 2:11 pm
by theda
Actually Burrito, what that guy suggested wasn't just changing them to .php, it included a code that pretty much states "If the current page isn't index.php, then redirect to it", which does what it needs.
Now I am curious as to how exactly I extract BLAH out of "BLAH.php" so that I can do the following "If the current page isn't index.php then redirect to index.php?id=BLAH" ... But I'm not quite sure how to extract the BLAH out of the BLAH.php...
Actually...
Posted: Mon Jul 11, 2005 3:25 pm
by mltsy
You can use the basename function for just that
basename("BLAH.php", ".php") takes .php as the extention and just returns BLAH.
So, basename($_SERVER['PHP_SELF'], ".php") would return "page" if they were looking at page.php

Of course, if they are looking at "index.htm" it would return "index.htm", not just "index".
Posted: Mon Jul 11, 2005 3:40 pm
by theda
For some reason, it comes back as an error saying
Warning: Wrong parameter count for basename()
Posted: Mon Jul 11, 2005 5:38 pm
by theda
Apparently my PHP is too out of date for , ".php" part to work... Any alternatives?
Edit: I have figured out the code, only after two hours of dull searching and editing... The code I came up with was this:
Code: Select all
<?
//Sets up the function for the current page
function strip_ext($name)
{
$ext = strrchr($name, '.');
if($ext != false)
{
$name = substr($name, 1, -strlen($ext));
}
return $name;
}
//Sets up a variable for the current page without extension
$redirect = strip_ext($HTTP_SERVER_VARS['PHP_SELF']);
//You can place $redirect in a link like so: page.com/?variable=$redirect
//This code will turn up page.com/?variable=page
?>
Posted: Mon Jul 11, 2005 5:59 pm
by John Cartwright
Code: Select all
function return_filename($file) {
$elements = explode('.',$file);
return $elements[0];
}
Assuming your filenames only contain 1 period.
Posted: Mon Jul 11, 2005 6:01 pm
by theda
I found another way also, as I edited my other post above with. I'll test your code once and see if it's any better than mine.