Setting the Apache 404 redirect for error page based on the language locale - apache

We are trying to set the error page redirection on apache based on the language . we are using the same virtual host for all the languages and all 404 pages are redirecting to only en_us language page . we want to redirect based on the language like ja_jp and de_de
We have tried setting the redirection rules and also location match . None of them are worked
using the locationmatch
ErrorDocument 404 /ja_jp/error/404.html
</LocationMatch>
<LocationMatch “^/en_us/(.*)”>
ErrorDocument 404 /en_us/error/404.html
</LocationMatch>
2) using the rewrite rule
RewriteCond %{ENV:REDIRECT_STATUS} 404
RewriteCond %{REQUEST_URI} ^/de_de/* [NC]
RewriteRule ^ /content/splunkcom/de_de/error/404.html [R=404,L]````

Related

.htaccess rewrite to show only root url and hide everything after /folder

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

How to keep website url the same in .htaccess 503 redirect

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.

htaccess: RewriteRule, Tomcat & 503

Given the following htaccess:
DirectoryIndex
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L,QSA]
RewriteRule ^ http://%{HTTP_HOST}:8080%{REQUEST_URI} [P]
If the Tomcat instance is running under Port 8080, the request will be passed.
If the instance is not running, I'm getting an ugly 503-error.
I was trying to replace it with a custom-made page. This works:
ErrorDocument 503 "offline"
but this doesn't:
ErrorDocument 503 /home/www-data/error/503.html
I'm getting:
Additionally, a 503 Service Unavailable error was encountered while trying to use an ErrorDocument to handle the request.
What is the correct solution?
For ErrorDocument you need to give the path of your html page, relative to the document root. So if the DocumentRoot is /home/www-data/ simply set /error/503.html.
Now if this is a .htaccess present in /home/www-data/ then all documents which are children of this directories get the rules applied. So I think even your /error/503.html is treated by your Proxy directive:
RewriteRule ^ http://%{HTTP_HOST}:8080%{REQUEST_URI} [P]
Would be instead:
RewriteCond %{ENV:REDIRECT_STATUS} !=503
RewriteRule ^ http://%{HTTP_HOST}:8080%{REQUEST_URI} [P]
I think it should be enough, or you can exclude /error from the matching path.

Canonical urls: Htaccess creates 403 error

I am trying to do a "www" redirects to non WWW for my site so I have canonical urls.
To do this, I have created the following .htaccess file:
//Rewrite to www
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
However, it creates the following error:
Forbidden
You don't have permission to access / on this server.
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
How can I get the www to redirect to non-www? I know I can use the canonical urls tag, but I would like to do it a more global level, than per a page.
My site is just a static HTMl site (it doesn't use PHP)
This is my site set up:
public_html/hosted/other/[siteroot]
The .htaccess is in [siteroot]

.htaccess url rewrite but nothing happens

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