Van
Van

Reputation: 29

throw 404 error with .htaccess file and php

I've got url like www.web.com/home and www.web.com/about with .htaccess file:

RewriteEngine On
Options +FollowSymLinks
ErrorDocument 404 http://www.web.com/page-not-found.html
RewriteRule "home" "index.php?c=home&m=main"
RewriteRule "about" "index.php?c=home&m=about"

If I type something like www.web.com/asd, .htaccess will throw 404 error and direct page-not-found.html file

But, If I type www.web.com/homesss or www.web.com/about/a/b/c, the .htaccess will not throw the 404 error

How do I fix that? I use pure PHP

Upvotes: 2

Views: 2156

Answers (2)

MrWhite
MrWhite

Reputation: 45829

In addition to @JarekBaran's answer...

If I type something like www.web.com/asd, .htaccess will throw 404 error

Not with the code you've posted...

ErrorDocument 404 http://www.web.com/page-not-found.html

When a request does not map to a resource, the above triggers a 302 (temporary) redirect to the http://www.web.com/page-not-found.html. There is no 404 HTTP response. Details of the request that triggered response are lost. /page-not-found.html is exposed to the end user. This is generally bad for SEO and delivers a bad user experience.

The 2nd argument to the ErrorDocument directive should be a root-relative URL-path, starting with a slash so that the error document is called with an internal subrequest. Apache then responds with a 404 status. The error document is not exposed to the end user.

(Very rarely should an absolute URL be used here.)

For example, it should be like this instead:

ErrorDocument 404 /page-not-found.html

Reference:

Upvotes: 2

user10946716
user10946716

Reputation:

Use meta characters in rewrite rule to 1:1 match:

^ --- Matches the beginning of the input

$ --- Matches the end of the input

[L] --- Flag last to stop processing rule after match

RewriteEngine On
Options +FollowSymLinks

ErrorDocument 404 http://www.web.com/page-not-found.html

RewriteRule ^home$ index.php?c=home&m=main [L]
RewriteRule ^about$ index.php?c=home&m=about [L]

Upvotes: 2

Related Questions