Jump to content

Recommended Posts

Hi,

 

I have the following htaccess file, because i would like to turn http://www.example.com/pageName into http://www.example.com/index.php?page=pageName.

 

This part works. however i have two things i would like to fix.

 

1) In my php code i check if the page excists in the database, otherwise i make a header to 404 using

header("HTTP/1.0 404 Not Found");

. It seems that the header not works, and i quess because of the RewriteRule. Does anyone knows how i would fix this? can i fix this inside the htaccess file?

 

2) secondly i notices that when i go to http://www.example.com/pageName/ (with a slash at the end) the page cant be found.

 

 

 

Options +FollowSymLinks
# IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
#  Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d


RewriteRule ([^\s]+)([a-z-\s]*) index.php?page=$1&display=$2

ErrorDocument 404 /error.php
ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 405 /error.php
ErrorDocument 406 /error.php
ErrorDocument 409 /error.php
ErrorDocument 413 /error.php
ErrorDocument 414 /error.php
ErrorDocument 500 /error.php
ErrorDocument 501 /error.php

 

 

Any help would be great!

Link to comment
https://forums.phpfreaks.com/topic/272210-rewriterule-404-error-page/
Share on other sites

My guess is that the header works, but you're actually showing the user the error page. Simply sending the 400 header to the client isn't enough, you also have to either redirect or include the error page as well. Otherwise whatever the default output is it'll be shown to the client.

After all, PHP doesn't know what sending any particular headers means, unless you tell it. ;)

The easiest way to check and see if it works, is by simply requesting the page and monitor the HTTP response. Most modern browsers (the exception being IE, and possibly Safari) have developer tools which allow you to not only inspect the DOM, but also the HTTP requests (and responses). There are also other tools that can do this, with cURL or wget being two such tools.

In short: Anything that displays the headers from the server will do.

 

As for the second question, try this:

RewriteRule ([^\s]+)([a-z-\s]*)/? index.php?page=$1&display=$2

Okay thanks. that works.

 

I tried the rewriteRule code but still getting 404 then (ending with a slash). Also, is it able to support upper and lowercases? when i try "pagename" instead of "pageName" i notice that some parts of my site missing...

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.