Forum Moderators: phranque
I am having a issue when using mod_rewrite to (doh ;)) rewrite URLs.
The rewriting part works, but typing anything that has not yet been rewritten the page loads without giving a 404 message (hope my explanation is plain enough).
Posting my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9]+)\.html$ /index.php?page=$1 [L]
</IfModule>
If anyone got a better way of getting the job done I would appreciate it very much.
Cheers!
I think it will throw a dangerous "302 Found" response.
Try it and see. Use the Live HTTP Headers extension for Firefox to see all of the servers responses (there may be more than one).
If there are chained responses, like 302 -> 404 or somesuch then run like hell.
You need the 404 response to be the first immediate response to the request.
* Valid Pages */
$valid_pages = array(
"",
"services",
"projects",
"about",
"contact",
"register"
);
/* Checking Valid Address */
$page = htmlspecialchars($_GET['page']);
if ($valid_pages !== $page && $page !== "")
{
HEADER("HTTP/1.1 404 Not Found");
include "/web/404.html";
exit;
}
The thing is that I am getting 404 where there actually is a match.
The one that is matching ATM is register, $_GET address is ?page=register that mod_rewrite translates to register.html, but unfortunately the if() seems to ignore that and gives a 404 instead. O.o
count($valid_pages)[b]-1[/b]; is probably needed. And you're comparing
$valid_pages[b][$i][/b] with the word coming in from the GET. Each time around the loop, do the compare and only set a flag if there is a match (like
IF ($valid_pages[$i] == $page) {$match="Y"; } or similar) The loop will run "i" times, so do not unset the flag if following iterations don't match.
After the loop, look at the flag. If it is NOT set (not set means that nothing matched), send the 404 page.
If it is set, continue with the script and send the real page of content.
[edited by: g1smd at 2:19 pm (utc) on Feb. 7, 2009]
Next time round it will send another 404, because "about" does not match "projects".
That's incorrect. Send either a 404 or the content only after you have done all of the matching for the whole list.
[edited by: g1smd at 2:34 pm (utc) on Feb. 7, 2009]
The loop runs to see if anything matches, comparing one URL at a time, and only sets the flag if a match is found.
After the loop (not inside it) a single compare looks to see if the flag was set. If it was not set, then nothing in the list was a match, and so the 404 page must be sent.
[edited by: jdMorgan at 3:34 pm (utc) on Feb. 7, 2009]
[edit reason] No URLs, please. See Terms of Service. [/edit]