I've tried this on two computers now and I've gotten the same results. Can people here try it as well to make sure I'm not crazy? And then help me understand why this happens? First, a really simple PHP page. <?php echo 'action => ' . $_GET['action']; ?> Code (markup): That's it. It prints the value of the query string parameter "action". Next, a rewrite rule. RewriteEngine on RewriteRule (.*) page.php?action=$1 Code (markup): Make the path, whatever that happens to be, the value for the action query string parameter. Seems simple. But what *always* prints, no matter what path I type in the address bar, is: action => page.php Code (markup): I really don't get it.
Hi, I'm new to this module too but I've found this tutorial which may help (I'm only half-way thru it myself...) http://tools.devshed.com/c/a/Web-Development/Module-mod-rewrite-Tutorial-Part-1/ They have the following example written on the 4th page: Which to me implies that the $1 & $2 appear like place holders - which you don't have? HTH, Cheers, p.
You have an infinite loop Use this instead: RewriteEngine on RewriteCond %{REQUEST_URI} !-f RewriteCond %{REQUEST_URI} !-d RewriteRule (.*) page.php?action=$1 Before, whatever was in the url was being rewritten to page.php?action=whatever and that itself was rewritten again to page.php?action=page.php ad infinitum.
Hi, krt. I tried your suggestion: RewriteEngine on RewriteCond %{REQUEST_URI} !-f RewriteCond %{REQUEST_URI} !-d RewriteRule (.*) page.php?action=$1 Code (markup): but the same thing still happens. pondlife, I do have the $1 placeholder. It's at the very end: ?action=$1
OK... krt, you were really close. This works: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule (.*) page.php?action=$1 Code (markup): And now the big question is: why? krt, you said something about an infinite loop. Why is the rewrite rule by itself a loop? Why isn't it a simple, one-time "replace this with that"? This solution also has a side effect I don't want. If the request points to an actual file, then it serves that file instead of applying the rewrite rule. But I don't want that. I want all requests to pass through my PHP page, and I want the PHP page to decide what to serve and what not. Any suggestions?
The rewrite rule by itself caused an infinite loop before because once the URL is rewritten, it is passed through the rules again and (.*) matches anything, including page.php (the file you are rewriting to). If you want the PHP file to handle everything, you can use this to rewrite everything except that PHP file: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !page\.php$ RewriteRule (.*) page.php?action=$1
Is that supposed to happen? I've been reading the mod_rewrite docs trying to figure my problem out, and I don't remember that bit.
As an aside, the rewrite condition had some minor defects. If the request was, for example, blah-page.php, or any other variation where page.php was the last part of the string, then the rewrite rule wouldn't be applied. (Not likely to happen, but I don't like hidden bugs.) I tried other variations. The closest I came was !^/page.php$ but that only worked when the .htaccess and page.php files were in the web-root directory. (REQUEST_FILENAME and REQUEST_URI are both full paths, never relative to the current directory.) Eventually I came up with this trickery: RewriteRule ^page\.php$ page.php [skip=1] RewriteRule (.*) page.php?action=$1 Code (markup): So if page.php is being requested, then leave the URL alone and skip the next rewrite rule. And it's relative to the current directory because it's just good old fashioned rewrite rules. I still think it's odd that I should even need to do this, but I guess I do, and so far this seems to be the simplest bug-free solution.
On second thought, shouldn't that trick in my last post also cause an infinite loop? Redirect to page.php -> if match page.php, then redirect to page.php -> if match page.php, then redirect to page.php -> if match page.php.... and on and on. So I guess I'm back to rewrite conditions. But there's still the problem that I can only test against absolute paths. Is there some solution for this?
Ah yep, I missed that I've just re-read this entire thread and I'm still no closer to understanding mod rewrite ...more reading of the docs required... I thought I'd be able to have it re-write my address bar URL's without having to change the way I've written my URL's in my sites code... Now I'm not so sure... Cheers, p.