Page 1 of 1

Help using mod_rewrite

Posted: Fri Jul 24, 2009 8:37 am
by insight
I'm using a tutorial (link) to help me understand how to use mod_rewrite so I can have more control over my webpage + links. But I'm having some issues with this basic mod_rewrite script.
.htaccess code

Code: Select all

RewriteEngine on
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
index.php code

Code: Select all

<html>
   <head>
      <title>Second mod_rewrite example</title>
   </head>
   <body>
      <p>
         The requested page was:
         <?php echo $_GET['page']; ?>
      </p>
   </body>
</html>
Now I have a directory listing like "http://www.insight.atbhost/mime_test/page/software" I have both index.php and .htaccess files in the mime_test folder and when I go to access the software folder it works and I get an echo saying: The requested page was: software. The problem is, the link in the address bar is still showing "http://www.insight.atbhost/mime_test/page/software" instead of "http://www.insight.atbhost/index.php?page=software" like I want it too.

I can also get different reading from the echo statement also. For example, if I change the "/mime_test/page/software" to "/mime_test/page/hardware" even if the hardware folder doesn't exists it still echoes The requested page was: hardware.

It also does the same if I manually put in /mime_test/index.php?page=software or /mime_test/index.php?page=hardware etc etc. It also doesn't load the index.php from those folders which I also want.

Can somebody please explain what I'm doing wrong?

Re: Help using mod_rewrite

Posted: Fri Jul 24, 2009 8:41 am
by insight
Would I be better off doing something like:

<?php
$page=$_GET['page'];

include("mime_test/page/" . $page . ");
?>

And write my links like http://www.insight.atbhost.net/index.php?page=software. That way whenever I click a link that directs me to that page it will include the php file from that folder where page="folder"?

Re: Help using mod_rewrite

Posted: Sat Jul 25, 2009 2:17 am
by cpetercarter
Your browser does not know what is happening on your server. It does not know that you have an .htaccess file which intercepts the url request and sends it somewhere else. So the address bar will continue to display the address which you entered into it - not the address to which .htaccess has diverted. If you want the address bar to display the "new" address, your php programme needs to send to the browser information to tell it the "new" address, typically by sending a "location" header.

The part of the url after the ? sends parameters to your php programme. The code which you have posted simply displays what the "page" parameter is. You have not asked the code to check whether the requested page actually exists, nor have you asked it load information specific to the page.

Re: Help using mod_rewrite

Posted: Sat Jul 25, 2009 2:45 am
by requinix
It's quite funny: you want to use mod_rewrite for the opposite of what most people want.

So, you want page/X to redirect to index.php?page=X but only when page/X actually exists?

Code: Select all

RewriteEngine On
 
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^page/([./]+)/?$ index.php?page=$1 [R]
The RewriteCond is saying that the filename (that corresponds to the request) is a directory, and the [R] in the RewriteRule means to do an external (via the browser) redirect.