Directory based error page redirection - .htaccess

I have my website with two localised content namely EN and FR and the content will be rendered through the URLs: http://www.example.com/en/ and http://www.example.com/fr/
I want to implement different error pages for each of these locale but do not I do not have an idea on how to implement through the redirect/virtual host configuration.
I read about different .htaccess file for each of the directory but that did not work.
And help will be good. Thanks in advance.

Having different .htaccess works very well, e.g.
/-+- en -- .htaccess
|
+- fr -- .htaccess
And in /en/.htaccess, you have
ErrorDocument 404 /path/to/en/error404.html
and the same with /fr/.htaccess
ErrorDocument 404 /path/to/fr/error404.html
You can also put error documents in the same directory and differentiate by extension, e.g. error404.en.htaml and error404.fr.html. You have then
ErrorDocument 404 /path/to/error404.en.html
and
ErrorDocument 404 /path/to/error404.fr.html
respectively.
If you don't want to use .htaccess files, you can achieve the same with Directory or Location directives. In your main or virtual host configuration, this would look like
<Directory "/path/to/example.com/en">
ErrorDocument 404 /path/to/error404.en.html
</Directory>
<Directory "/path/to/example.com/fr">
ErrorDocument 404 /path/to/error404.fr.html
</Directory>
The <Location> directive looks similar.

On Apache 2.4 you can use expressions:
# for /fr/ URIs
<If "%{REQUEST_URI} =~ m#^/fr/#">
ErrorDocument 404 /fr-404.html
</If>
# for /en/ URIs
<If "%{REQUEST_URI} =~ m#^/en/#">
ErrorDocument 404 /en-404.html
</If>
Check Apache 2.4 expression reference.
If you're still stuck with Apache 2.2 then you can use:
RewriteEngine On
# for /fr/ URIs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fr/ /fr-404.html [L]
# for /en/ URIs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en/ /en-404.html [L]
However keep in mind that Apache 2.2 solution will return 200 status to clients.

Related

Send to non Drupal 404 for path in htaccess

I have a Drupal 8 site and I want to send a certain path to hit a non Drupal 404 (Don't want it to hit Drupal at all so nothing is logged). For example: autodiscover/autodiscover.xml. I tried the following:
I tried the following 3 things in .htaccess but they did not work:
SecRule REQUEST_URI ".*autodiscover/autodiscover.xml" "id:9990001,nolog,status:404,chain"
SecRule SERVER_NAME "mysite\.com" "t:lowercase"
RewriteRule ^autodiscover/autodiscover.xml?$ - [R=404,NC,L]
ErrorDocument 404 autodiscover/autodiscover.xml
This should work:
RewriteCond %{REQUEST_URI} /autodiscover/autodiscover.xml [NC]
RewriteRule ^ - [F,L]
https://support.acquia.com/hc/en-us/articles/360004553253-Avoiding-404-error-messages-in-your-logs

too many redirects with .htaccess and a very simple configuration

Good day all.
I'm doing a simple dashboard o a site, I've set up a very simple htaccess to handle some URLs:
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
#routing:
RewriteRule ^login/?$ login.php [L]
RewriteRule ^page/([0-9]+)/?$ /index.php?page=$1 [QSA,NC,L]
#errors:
ErrorDocument 404 /404.php
As far as I know this should be quite straight forward, but:
going on /page/1234 URL works fine (as well as any other number used, so the pattern is working).
going on www.example.com/login OR www.example.com/login/ is causing:
1) too many redirects error if the "errorDocument" line is on top of everything
2) a /404.php redirect if the "errorDocument" is at the bottom of the htaccess.
while, I can access directly /login.php without any problems.
i've done some tests but I can figure out what is going wrong, I've also tested the file with this tool:
Converting my comments to answer.
Looks like you have MultiViews option turned on and getting this unexpected behavior.
Options -MultiViews
Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will try to resolve it and serve /file.php.

htaccess redirect empty uri instead of directory listing

If I just do Options -Indexes
localhost/subdir/ would give me 500 error.
But I want to redirect it to https://github.com/miranda-zhang/cloud-computing-schema
Something like
RewriteRule ^$ https://github.com/miranda-zhang/cloud-computing-schema [R=308,L]
Currently, this doesn't work on my local server.
I have to do
RewriteRule ^home$ https://github.com/miranda-zhang/cloud-computing-schema [R=308,L]
And use the url localhost/subdir/home
Also Options -Indexes seem to make the following stop working.
ErrorDocument 406 https://miranda-zhang.github.io/cloud-computing-schema/v1.0/406.html
RewriteRule ^.*$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/406.html [R=406,L]
Full original .htaccess file
I also tried some methods in Problem detecting empty REQUEST_URI with Apache mod_rewrite
The following doesn't seem to work, or maybe I'm missing some other config.
RewriteCond %{HTTP_HOST} ^localhost/coocon
Or
RewriteCond %{REQUEST_URI} "^/$"
Seems something else was wrong, it is a 403 error now.
Forbidden
You don't have permission to access /cocoon/ on this server.
Cannot serve directory /var/www/html/cocoon/: No matching
DirectoryIndex (index.html,index.cgi,index.pl,index.php,index.xhtml,index.htm) found, and server-generated directory index forbidden by Options directive
This might work:
Options -Indexes
ErrorDocument 403 https://github.com/miranda-zhang/cloud-computing-schema

HTACCESS: Throw 404 error but still accessible

I have the following structure:
- root
- css
- test.css
- index.php
Is there a way to throw a 404 error when I try to open anything in CSS folder in my browser as:
http://localhost:1993/css
http://localhost:1993/css/test.css
but still accessible from the browser? I want CSS to load normally, but to throw a 404 error when someone opens this path. Or turn off the directory listing for everything in the directory (with all files inside)? Or something like that? Its possible?
You can try mod_access,
<FilesMatch "\.css$">
order deny,allow
deny from all
allow from 127.0.0.1
allow from localhost
</FilesMatch>
With mod_rewrite
RewriteEngine On
RewriteCond %{HTTP_HOST} ^!localhost
RewriteCond %{REQUEST_URI} ^/css [OR]
RewriteCond %{REQUEST_FILENAME} \.css$
RewriteRule ^ - [R=404]

Rewrite path before processing directory?

I'm having a small problem with my htaccess files. Currently, it redirects everything back to index.php for processing, except when someone tries to access an actual directory. When the directory exists, it displays the 403 error page instead of rewriting the path to index.php like it's supposed too. What can I modify to make it always go to index.php when the files are accessed via the internet, but still load the correct files in PHP on the server?
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
ErrorDocument 403 /index.php?403
ErrorDocument 404 /index.php?404
ErrorDocument 414 /index.php?414
RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteRule !^(.*) index.php [L]
Sample file structure:
Web Directory
- com
- source
- index.php
- TEST.HTML
The folders such as 'com' and source' will display the 403 because the user doesn't have access to them. The files such as 'index.php' and 'TEST.HTML' execute normally. I want my htaccess to redirect everything in the Web Directory folder back to the index.php file, no matter what.
I think you want this instead:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
ErrorDocument 403 /index.php?403
ErrorDocument 404 /index.php?404
ErrorDocument 414 /index.php?414
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule .* index.php [L]
This was on the assumption that you didn't want to be able to access TEST.HTML directly and didn't want to change the URL in the user's browser. If either of those assumptions were wrong, let me know and I'll update the answer with the appropriate rewrite information.

Resources