Remove File Extention from Filename [Solved] (finally)
Moderator: General Moderators
Remove File Extention from Filename [Solved] (finally)
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.
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.
Last edited by theda on Mon Jul 11, 2005 6:03 pm, edited 2 times in total.
using $_SERVER['PHP_SELF']
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.
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.
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 ^_^;
Edit: Nevermind, I remembered $HTTP_SERVER_VARS is the version I use <_< Outdated POS server ^_^;
Last edited by theda on Mon Jul 11, 2005 2:01 pm, edited 1 time in total.
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.
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.
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...
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...
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".
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".
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:
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
?>
Last edited by theda on Mon Jul 11, 2005 6:00 pm, edited 1 time in total.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
function return_filename($file) {
$elements = explode('.',$file);
return $elements[0];
}