Call my custom 403 page from htaccess file - .htaccess

Is there a way to call my custom 403 page from my htaccess file as I do with my custom 404 page? The way I do with my 404 page is:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /404.php [L]
So, I want to do the same with my 403 page, not using "ErrorDocument 403 /403.php".
Thx.

There is no way using Rewrite Rule, because 403 is the Forbidden status code. And you canĀ“t rewrite this like your 404.
You have to use "ErrorDocument 403 /403.php".

Related

.htaccess extensionless URLs + ErrorDocument 404 issue

I use the following code to serve extensionless URLS. By that I mean anyone requesting myfile.php will be served myfile
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I also use the following so I can use a custom 404 page:
ErrorDocument 404 /error.php
All that works fine until someone types in an extentionless URL which doesn't exist, such as "/abcdefghijkl" In this case Apache returns a blank page with "File not found" which isn't very helpful to users! In such a case, I want the error page (error.php) to be displayed. Is it possible?

Redirect to 404 page from forbidden page when url has only extension without page name

I am facing 403 error forbidden message when accessing no page name with extension.
Ex. http://loremipsum.lorem/.html shows 403 forbidden.
Used in .htaccess file
RewriteRule \.html$ - [R=404,L]
Also used
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ test.php?page=$1 [L]
Whenever anyone tries to access /.html or /.htm or /.php in url, web page should be redirected to 404 page or any custom page (set any custom page instead of default error page).
/.html or /.htm or /.php are not valid addresses, so set ErrorDocument 404 /404.html in your .htaccess file and create /404.html file with your custom error message.

How to handle 403 errors without redirecting to another page

My htaccess file currently handles 404 errors as following:
Options +MultiViews
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ 404.html [L]
I did not use the ErrorDocument, because it redirects to an absolute error path.
I want to stay at the same url but show a custom template for a 403. Currently the 403 shows the default error page of apache... I would like to show a custom error document here too.
How would I do this?
Thanks for your help ;)
You can use ErrorDocument with a relative target path . With a relative path ErrorDocument directive won't redirect your request but instead show the error page without changing the url.
ErrorDocument 403 /403.php

htaccess - redirect if rule doesn't exists

I'd like to ask - is there a way to auto redirect with httacces conditions when rewrite rule doesn't exists?
For example.
I have the following rules:
RewriteRule ^/index/$ index.php
RewriteRule ^/error-404/$ 404.php
ErrorDocument 404 /error-404/
When someone will try to access adress like index/something-added-by-user/ he will see 404 page, but url will be still "index/something-added-by-user/". Is there a way to automatically redirect from that url typed by user to url "error-404/"? So when he type url like above, he will see 404 page with 404 page url.
It can be done using this rule:
# not a real dir
RewriteCond %{REQUEST_FILENAME} !-d
# not a real file
RewriteCond %{REQUEST_FILENAME} !-f
# not a real link
RewriteCond %{REQUEST_FILENAME} !-l
# redirect it to 404.php
RewriteRule ^ /404.php [R,L]
Though keep in mind that browser will not get 200 status instead of 404.

htaccess 404 redirect not working

I am trying to setup a custom 404 page for my website using: ErrorDocument 404 /404.html but when I add this, going to domain.com/dslkjflsdkjflskdjf doesn't redirect to the 404 page. Instead, it redirects to my homepage where mostly everything on the page is broken.
I'm wondering if this mod_rewrite.c module is the cause of this, I've tried modifying it (taking L out, adding the redirect inside the module, etc) but no luck.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>
I'm not sure about this but I think your server won't recognize a 404 error because every request is redirected to your index.php. You have to determine in the index.php if there is a 404 error and send the header with the header() function.

Resources