In an .htaccess file I can do something like ErrorDocument 404 /error/404 to give a friendly error message. I'd like to do the same thing for other status codes, but I don't want to have to enter all of them in the htaccess file.
So is there a way to do a rewrite on the status code? Something like:
ErrorDocument [0-9]* /error/$1
No, you can't rewrite your error document like that.
You need to use a RewriteRule to rewrite your error Document.
RewriteRule ^([0-9]{3})/?$ /error/$1 [NC,L]
Related
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
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]
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:
https://httpd.apache.org/docs/2.4/mod/core.html#errordocument
I have a link like this:
http://www.expamle.com/folder1/folder2/folder%20/file.html
I want to 301 redirect it with .htaccess to:
http://www.expamle.com/folder1/folder2/folder/file.html
I tried this:
RewriteCond %{REQUEST_URI} ^\/folder1\/folder2\/folder\%20\/file\.html$
RewriteRule .* http://www.example.com/folder1/folder2/folder/file.html [R=301,L]
but I'm getting:
Not Found
The requested URL /folder1/folder2/folder /file.html was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
I also tried this
Redirect 301 /folder1/folder2/folder%20/Export1.htm http://www.example.com/folder1/folder2/folder/file.html
but got the same error.
What am I doing wrong?
The solution is to add quotes like this:
Redirect 301 "/folder1/folder2/folder /Export1.htm" http://www.example.com/folder1/folder2/folder/file.html
before to start asking i want to say that i tried to search everywere and i didn't found anything so...
My problem is that i want to have change first.html second.html and so on to first second without extensions and i use this:
Options +MultiViews
and now i have my files without extensions, now i want that when someone write first.html or second.html send it to error page, now my code is the following:
ErrorDocument 404 errore/errore.html
Options +MultiViews
Hope you help me.
Try adding:
RewriteCond %{THE_REQUEST} \ /+[^\?]+\.(html|php)
RewriteRule ^ - [L,R=404]
This rewrite rule (you'll need RewriteEngine On if you don't already have that in your htaccess file) matches direct requests to anything that ends with the extension .html or .php and flags the request as a 404 response using the R=404 rewrite flag.
I want to redirect all pages on my site (including index) to 404 using htaccess.
Thanks in advance.
You can use this rule in root .htaccess file:
RewriteEngine On
RewriteRule ^ - [R=404,L]
The following works on newer apache:
Redirect 404
And you can add a message without creating a file like this:
ErrorDocument 404 "Hmm... There's nothing here..."
A quick question. I want to use a php file that will deliver pages based on the url. I am using htacess to deal with the error docs but my question is to do with SEO. If I redirect a 404/403 request to pageDispacher.php that then delivers the correct page will the header be a 404/403 at any point? As this is not cool. Do I need to use a rewrite rule instead?
In which case is there is a genuine 404 page how would i return a 404 header?
www.example.com > www.example.com/en/home
www.example.com/en/ > www.example.com/en/home
ErrorDocument 404 /pageDispacher.php
ErrorDocument 403 /pageDispacher.php
This is usable:
RewriteRule ^error error.php [L,QSA]
RewriteRule ^(.*)\.php$ error[L,QSA]