Send to non Drupal 404 for path in htaccess - .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

Related

URL changes when using ErrorDocument 403 in .htaccess

I have written a .htaccess file to make sure that visitors are always redirected to https and to www. In addition to that I have also added a custom html page for 404 errors.
When visitors try to access a forbidden file I want them to see my custom 404 message, as to not reveal that the path contains a forbidden file.
Here is the problem. When writing for example "example-domain.com/.htaccess" (no www or https) in the browser, the URL in the address field in the browser changes to "https://www.example-domain.com/missing.html". But I want it to say "https://www.example-domain.com/.htaccess" while displaying my 404 page.
It works for 404 errors. But when typing in a path in the address field which both triggers the 403 error and fulfill at least one of the rewrite conditions in my .htaccess file (missing https and/or www) I experience the above described problem.
Here is the code in the .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^example-domain.com [NC]
RewriteRule ^(.*)$ https://www.example-domain.com/$1 [L,R=301,NC]
ErrorDocument 403 /missing.html
ErrorDocument 404 /missing.html
Best regards

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

Directory based error page redirection

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.

.htaccess domain redirection to folder but preventing direct access with 404

I have a shared hosting in which I want to host several websites. To this end, and to keep things clear, I have redirected my primary domain (i.e. www.mydomain.com) to a folder (i.e. /mydomain.com/). However, I want to prevent direct access to this folder (www.mydomain.com/mydomain.com/) from the URL with a 404 error (not found).
This is the .htaccess in the root directory:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain.com/0.1/$ [NC]
RewriteRule ^(.*)$ /mydomain.com/0.1/$1
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteRule ^(/)?$ mydomain.com/0.1/index.php [L]
RewriteCond %{THE_REQUEST}% ^[A-Z]{3,9}\ /mydomain\.com/ [NC]
RewriteRule . - [R=404,L]
And this is the .htaccess in /mydomain/ with a bunch of rules for nice and tidy URLs.
RewriteEngine On
RewriteRule ^about/(.+) index.php?a=about&b=$1
RewriteRule ^services/(.+) index.php?a=services&b=$1
Right now this is the result:
Accessing www.mydomain.com shows the content in /mydomain/ as hoped. However, www.mydomain.com/mydomain/ is also displayed, ignoring the 404 rule, and creating another folder -whatever the name- without an htaccess DOES throw the 404.
I've been dealing with this problem for 5 days now and tried everything. At this point I don't know if the error comes from the root .htaccess or the folder's.
Thanks in advance.
PS: Just to be clear, I have no control over the httpd.conf file.
Notes:
I forgot saying that there is an additional folder inside /mydomain.com/ called /0.1/ for version control.
If a include "RewriteOptions Inherit" after "RewriteEngine On" in the /mydomain.com/0.1/ htaccess, I get a 500 internal server error.
Deleting the /mydomain.com/0.1/ htaccess file altogether will produce the desired 404 error for www.mydomain.com/mydomain.com/0.1/ and produce a 505 in www.mydomain.com
The R flag in rewrite rule should be something between 301 to 399 , so it can not be 404!!!
your rule should be like below:
RewriteCond %{THE_REQUEST} ^/mydomain\.com/? [NC]
RewriteRule (.*) throw_404.php [L]
create throw_404.php and to send 404 status code! and add L flag to your first rule for avoiding conflicting between that rule and mine!

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