Jump to content

Recommended Posts

Guest LH91325

It was a heck of a lot simpler than that for me. This goes in my .htaccess file:

RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /

That makes all requests go through the index.php. The one script is the head of a PHP-MySQL HTML generator and all the code on the site comes from that one script. (It adds appropriate processing by including other scripts as required). And then don't use any HTML links that refer to "index.php" Also, I've got all my ErrorDocument processing going there too. Note that your index.php will have to do its own URI deconstruction to get directory names, file names, etc. if you use that stuff. (I do.) It works just like the WordPress code, a good example of how to get rid of .PHP in your links and search engine listings.

Edited by LH91325
Link to post
Share on other sites

i found my .htaccess file and read a lot about it i now have my 404 error page modefiyd ;) and am super happy about that ;) but i still can not remove the .php from the url, i tryed... RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . / but it did not work, is there any more code needed in .htaccess?

Edited by westman
Link to post
Share on other sites
Guest LH91325

The method above works if you funnel all your traffic including error pages into just one index.php. As I said earlier, doing that may be overkill. As it happens the single index works very well for me, but it's not a single solution for every site design problem/situation. One example of this being done is WordPress. In theory you could have a single index.php file which looks at $_SERVER['REQUEST_URI'] and based upon that includes an appropriate php file to handle that request. For example your link might be: www.example.com/some_page or it could be www.example.com/some_page/ You don't actually have any page like that so Apache decides page not found, and activates the rewriting noted in your post above, which sends the request to / which has only one index, that's your index.php. When your index.php executes it looks at the request URI and sees the words "some_page" and then can include and execute your script, perhaps some_page.php (although you can call it anything you want). If you do that you'll never see any .php in your URLs (unless you specify one in a link, so don't do that). Perhaps there is a way to use .htaccess directives to hide .php but I don't know how, nor do I know if it's possible. All I know is things that are possible. I would have to know everything before I could say something is impossible, and for sure I will never know everything.

Edited by LH91325
Link to post
Share on other sites
Guest LH91325

Here is a working example of how you could accomplish that: index.php

<?php$uri = trim($_SERVER['REQUEST_URI'], '/');if (file_exists($uri . '.php')) { require($uri . '.php'); exit; }header("HTTP/1.x 404 Not Found");die("<HTML><BODY><H1>Not Found</H1><P>Sorry, our server was not able to locate the document that you requested.</P></BODY></HTML>");?>

some_page.php

<?phpecho "some page";?>

With the above files and .htaccess as shown in post #8 you can access www.example.com/some_page or www.example.com/some_page/ and you'll get:

some page
Enter any other URL and you'll get 404 not found. Note that you would probably want to add additional complexity to serve your index page content.
Link to post
Share on other sites
Guest LH91325

Here, this would probably work:

<?php $uri = trim($_SERVER['REQUEST_URI'], '/'); if (file_exists($uri . '.php')) { require($uri . '.php'); exit; } if ($uri == '') { require('your_index.php'); exit; } header("HTTP/1.x 404 Not Found");die("<HTML><BODY><H1>Not Found</H1><P>Sorry, our server was not able to locate the document that you requested.</P></BODY></HTML>"); ?>

The script your_index.php would handle what you want them to see when they visit your main site. Note that you should improve this example with more security handling. For example, restrict visitors' ability to enter unessential information in the URL:

$uri = preg_replace("/[^0-9A-Za-z_-]/", '', utf8_decode($uri));

You can protect your scripts from visitors typing script names directly. Add this to your index.php:

define('WHATEVER', true);

Then check if it has been defined in every other included script:

if (!defined('WHATEVER')) die();

By the way, the "exit" statements in the first example were security to handle what happens if one of your included scripts returns instead of exiting. You would probably want to make sure that all the included scripts exit when they are finished.

Link to post
Share on other sites

nice, i have a question about post NO.10 will it work on my site?my site is setup as...www.mysite.comwww.mysite.com/indexwww.mysite.com/userwww.mysite.com/user/gameswww.mysite.com/user/games/homei also use $_GET, $_POST, $_REQUEST & $_SESSION (in php) will it work?

Link to post
Share on other sites
Guest LH91325

You can make it work but you'll need to either repeat the same thing in each directory, or in your basic directory you'll have to disassemble the URI and decode the directory names to decide which scripts to activate. That's what I do on my site. It looks like there are several directories if you look at the site structure from the point of view of visitors, but there are no directories.

$uri = trim($uri, '/'); // get rid of any end slasheslist($first_level_directory, $second_level_directory, $third_level_directory) = explode('/', $uri);// now $first_level_directory and the rest have the requested directory names// or they'll be empty if there's no directory requested at that level

For example on your last example, $first_level_directory == 'user'$second_level_directory == 'games'$third_level_directory == 'home' You an use various conditional logic to steer requests to the right subscripts. $_GET, $_POST etc. work just fine. As execution passes to different scripts the context of the variables goes with them. They are simply included scripts. As far as the PHP processor is concerned they are just extensions of the same script. Just one script split across several PHP files. You might be better off in a simple site to just accept having .PHP appear in your URLs. However at some point if you use logical hierarchical design techniques you will reach the point where it's simpler to have a single index file and pass everything through that. A good example is WordPress. They are long past the point where they could have used separate scripts. Use of a single index file actually makes WordPress simpler in terms of code complexity. The example above is just to show the concept. For example if you are passing variables in the URI (like you have ?variable=something&variable2=something_else) then you'll have to strip that out before handling the directories. You'll learn a lot about writing PHP as you figure that stuff out.

Edited by LH91325
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...