From this URL
www.example.com/error_documents/404
to this URL
www.example.com/404
I've tried many different .htaccess rules but none of them worked.
I'm trying to just hide the /error_pages/ folder section from the URL without any actual redirecting because if I write a correct *RewriteRule, its just keep repeating itself and I get an ERR_TOO_MANY_REDIRECTS error, because if you want to go to an unknown folder, the error document redirects to example.com/error_documents/404 and if I rewrite this to example.com/404, its an unknown folder so it is trying to redirect me to the /error_documents/404 page but the htaccess file keeps redirecting to a forever loop.
Current .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+error_pages/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^error_pages/)^(.*)$ /error_pages/$1 [L,NC]
This .htaccess gives me forever loop:
www.example.com/unknownfolder
to
www.example.com/error_documents/404
to
www.example.com/404
and this keeps repeating...
I'm using cPanel for ErrorDocuments and the main .htaccess file is:
ErrorDocument 400 http://example.com/error_pages/400
ErrorDocument 401 http://example.com/error_pages/401
ErrorDocument 403 http://example.com/error_pages/403
ErrorDocument 404 http://example.com/error_pages/404
ErrorDocument 503 http://example.com/error_pages/503
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ErrorDocument 400 http://example.com/error_pages/400
ErrorDocument 401 http://example.com/error_pages/401
ErrorDocument 403 http://example.com/error_pages/403
ErrorDocument 404 http://example.com/error_pages/404
ErrorDocument 503 http://example.com/error_pages/503
You shouldn't be using an absolute URL in the ErrorDocument directive in the first place - this is what is causing the external (302) redirect and exposing the location of /error_pages and the error document. Consequently, this also loses information about the request that caused the error.
However, /400 and /401 etc. should reference the actual file(s) that handle the request. eg. /400.html and /401.html etc.
You should be using a root-relative file-path (starting with a slash) to the error document and then Apache will issue an internal subrequest, rather than a redirect.
For example:
ErrorDocument 400 /error_pages/400.html
ErrorDocument 401 /error_pages/401.html
ErrorDocument 403 /error_pages/403.html
ErrorDocument 404 /error_pages/404.html
ErrorDocument 503 /error_pages/503.html
Your /error_pages now remains totally hidden from the end user.
No need to manually try and remove this from the URL (because it should never be present in the URL to begin with). You can (optionally) prevent direct access to the /error_pages directory if you want (careful not to block subrequests for the error documents).
Reference:
https://httpd.apache.org/docs/2.4/mod/core.html#errordocument
Related
I have a .htaccess file and i've set up a 'Coming Soon' website. It excludes my ip as i'm the developer but for other visitors I don't wan't it to change the url of the address.
Here's the file:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^12.345.67.89$
RewriteCond %{REQUEST_URI} !/HTML/pages/construction.html
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|ico|mp4) [NC]
RewriteRule ^(.*)$ /HTML/pages/construction.html [R=302,L]
ErrorDocument 404 /HTML/error-pages/404.html
How can I do this? Help is very much appreciated
If you don't want to change the URL then simply remove the redirect flag from your RewriteRule .
Do the following :
Change
RewriteRule ^(.*)$ /HTML/pages/construction.html [R=302,L]
To
RewriteRule ^(.*)$ /HTML/pages/construction.html [L]
There is no "503" response in the code presented here unless you are manually setting the HTTP response status in your server-side script. But if this is a .html file then that seems unlikely.
To correctly serve a "503 Service Unavailable" response you should define the appropriate ErrorDocument and call this using the R flag.
For example:
Options +FollowSymlinks
ErrorDocument 404 /HTML/error-pages/404.html
ErrorDocument 503 /HTML/pages/construction.html
RewriteEngine On
# 503 Service Unavailable except for the given IP address
RewriteCond %{REMOTE_ADDR} !^203\.0\.113\.111$
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule !\.(jpe?g?|png|gif|css|ico|mp4)$ - [NC,R=503,L]
Despite the use of the R flag, there is no external redirect here. (A redirect only occurs for status codes in the 3xx range.)
The condition that checks against the REDIRECT_STATUS environment variable does two things:
It ensures that an internal subrequest for the 503 ErrorDocument itself doesn't trigger a 503 - which would result in an endless loop and no custom ErrorDocument is returned in the response.
A direct request for the /HTML/pages/construction.html document (the 503 ErrorDocument) will itself trigger a 503 response.
Also note that if you are sending a 503 response, you should ideally be sending a Revisit-After HTTP response header as well to indicate to (search engine) bots when your site will be available.
I am trying to redirect to a custom 404 page when a file is not found. However my server is responding with "File Not Found" but doesn't say 404 and doesn't redirect.
I am using amazon Lightsail using the LAMP bitnami stack.
It redirects successfully on a 403 error, and it redirects successfully when I try to navigate to a invalid directory. The invalid files however don't get handled correctly. They just merely print "File not found"
Here is my .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ErrorDocument 400 https://example.com/error.php
ErrorDocument 404 https://example.com/error.php
ErrorDocument 403 https://example.com/error.php
ErrorDocument 500 https://example.com/error.php
Options -Indexes
# SPELLING
<IfModule mod_speling.c>
CheckSpelling off
</IfModule>
Found the answer.
Added this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /error.php [L,R=301]
Now it works for anyone curious.
Im currently trying to rewrite the urls of my new site by writing im my .htaccess. ive done redirects for 3 different scenarios and a generel.
The urls doesnt get redirect for nice url.
rewriting this url "http://domain.dk/?fag=1&id=87" to: "http://domain.dk/fag/1/id/87"
rewriting this url "http://domain.dk/?fag=1" to: "http://domain.dk/fag/1/"
rewriting this url "http://domain.dk/?id=87" to: "http://domain.dk/id/87"
my code is
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domain\.dk$ [NC]
ErrorDocument 400 /?fejl=400
ErrorDocument 401 /?fejl=401
ErrorDocument 403 /?fejl=403
ErrorDocument 404 /?fejl=404
ErrorDocument 500 /?fejl=500
RewriteRule fag/(.*)/aflevering/(.*)/ index.php?fag=$1&id=$2
RewriteRule fag/(.*)/ index.php?fag=$1
RewriteRule aflevering/(.*)/ index.php?id=$2
You need the AllowOverride directive set to All for the container
Is it possible for Apache to process errors before any RewriteRule?
I have the following:
RewriteEngine on
RewriteRule ^(.*).(htm|html)$ /cgi-bin/processor.py?file=$1.$2 [L]
So any .htm(l) files are sent to a python script. However, before that happens, I'd like any errors, such as a 404 to be checked for. That way, if I go to fakeFile.html I get my ErrorDocument 404 page, instead of having to handle that in my script.
Is that possible?
Thanks.
You can use:
# handle 404 error
ErrorDocument 404 /404.html
RewriteEngine on
# rewrite only if .html file exists
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+?\.html?)$ /cgi-bin/processor.py?file=$1 [L,QSA,NC]
I'm trying to only allow https connections to a subdomain of mine. My .htaccess looks like this currently:
Options -MultiViews -Indexes
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
RewriteEngine On
#Only allow https requests
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !/docs/?(.*)$ [NC]
RewriteRule ^ - [F]
#Docs file rewrite
RewriteRule ^docs/?$ /Docs.php
If I remove the Only allow https requests section and try to access a directory (e.g. http://foo.bar.com/images/), I am displayed my pretty 403 page. But if I leave that section in and go to the subdomain root (e.g. http://foo.bar.com/), I get the default error page, is this something to do with htaccess completely forbidding access to all files?
Examples:
Through a secure connection the 403 page works - https://api.subjectplanner.co.uk/assets
But through a non secure one, it is the default page - http://api.subjectplanner.co.uk/assets
RewriteEngine On should be before you use rewrite rules:
Options -MultiViews -Indexes
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
RewriteEngine On
#Only allow https requests
RewriteCond %{HTTPS} off
RewriteRule !^docs - [NC,F]
RewriteRule ^docs/?$ /Docs.php [L,NC]