Apache - Use a unique index.php with existing folders - apache

I want to redirect all the pages of my website (non existing files and folders) to a unique index.php. It's simple, this is my .htaccess into the root of my website:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
ErrorDocument 400 /index.php?er=400
ErrorDocument 401 /index.php?er=401
ErrorDocument 403 /index.php?er=403
ErrorDocument 404 /index.php?er=404
ErrorDocument 406 /index.php?er=406
ErrorDocument 408 /index.php?er=408
ErrorDocument 411 /index.php?er=411
ErrorDocument 500 /index.php?er=500
It works fine for non existing files and folders BUT I need to keep some real folders to store files and I get a 403 Forbiden error when I call "site.com/folder/"
For example if folderA doesn't exists, every call to "site.com/folderA/..." works and I have a 200 OK response for the folder, subfolders and files.
But if folderB exists, I get 404 for all non-existing files and 403 for non-existing subfolders instead of the redirection to my main index.php.
What solutions Do I have to keep thoses folders and redirect all non existing urls to my index ?
Thank you

If you do not only want all non-existing folders to be routed to your index.php (which your rewrite rules will achieve), but the existing ones as well, instead of getting a 403 for those, you can simply use the DirectoryIndex directive like this:
DirectoryIndex /index.php
DirectoryIndex does not only accept a file name, but a local URL as well – and the leading slash makes it relative to the domain root.
So for all your existing folders, your index.php located at the domain root will now be served as the index document.
This could of course be achieved by other means as well – but using mod_rewrite for this would involve more checks, and using ErrorDocument 403 would still write an entry into the server’s error log for every existing folder you request.

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

.htaccess skips mod_rewrite and directs to ErrorDocument 404

I have the following .htaccess which works wonderfully on a few websites of mine. However, I have uploaded it to another website (on the same host, different domain) and it is now defaulting to the error 404 page; which is displaying correctly.
Example URL: https://www.example.ca/resources-and-links/documents/
The .htaccess first checks to see if there is a actual .php file with first sub-directory, in the example resources-and-links.php. If it does exists it will serve up that page and break down the rest of the sub-directories into the query strings provided.
If the resources-and-link.php doesn't exsits, it directs it to the content.php to check it against pages in the database and serve it if the url matches one of that in the database. If it doesn't the content.php page shows a custom error 404 page.
This works on a sub-domain of said website https://sub.example.com which runs the cms system I built but not the root domain and as said before; shows the ErrorDocument instead. As well, I've used this same .htaccess on many of my other websites without a problem.
I used PHP to show that mod_rewrite is available so are there any reasons why it would not work on the root domain? Why is it skipping straight to the ErrorDocument and serving /404.php?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([^/]*)
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([^/]*)(/([^/]*))?(/([^/]*))?(/([^/]*))?(/([^/]*)) /$1.php?ax=$3&do=$5&third=$7&fourth=$9 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/(.*)$ /content.php?url=$1/$2 [L]
Options +FollowSymLinks
ErrorDocument 404 /404.php
</IfModule>

Redirect all request to index.php with ErrorDocument 404

I want to use one index.php file for all requests, except for files that exist eg. images, CSS and JS.
Also I want to force trailing slash on all requests.
Also I want to have one folder with system files that should not be accessible. But I don't want any one to see that this folder or these files exist.
Also I want all this to be as fast as possible.
I figured that I could use this htaccess:
RewriteEngine On
# Force trailing slash on the end
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
# Handle all request from one index.php file
ErrorDocument 404 /index.php
# Prevent system folder from being accessed
RedirectMatch 404 ^/system/?$
In index.php I will decide if page exist or not and set response code accordingly.
I'm no pro, but it seems to work. But I wonder if ErrorDocument 404 /index.php will slowing anything down, eg. affecting how the server cache the request in the RAM memory?

.htaccess issue when including custom 404 error page

This is my first time using .htaccess and currently my .htaccess file is redirecting to my custom 404 page. This is being handled with the following line in my .htaccess file:
ErrorDocument 404 /404.html
On top of this, I need to rewrite a few urls so that /addition/ points to /includes/addition.html (this is one example). So I add the following:
RewriteEngine On
RewriteRule /addition/ /includes/addition.html
But this then serves me a 500 error when Is hold be getting 404 error. On top of this, when I point to mysite.com/addition/ the browser isn't fetching addition.html from my includes folder.
Would someone please explain to me how to have these two rules working without effecting the other, and correct my secondary rewrite rule?
Danke.
Have it this way in your site root .htaccess:
ErrorDocument 404 /404.html
RewriteEngine On
RewriteRule ^/?([a-z]+)/?$ /includes/$1.html [L,NC]

.htaccess prevent folder access that are subdomains

in my webhost every time I create a new subdomain it creates a public-access folder in my html_public folder, and I want to block the access to those folders with a fake 404 error (using the default one if it's possible, I'm using this to set the default 404 error: ErrorDocument 404 /404.php) but allowing to access if it is the subdomain.
For example:
http://www.mydomain.com/blog/ -> Should show a 404 Not found error
http://blog.mydomain.com -> Should allow the user to access
Both of them are the same folder html_public/blog
I tried to add a .htaccess file in blog folder with this code:
order deny,allow
deny from all
But it does not allow the access (logic), it does not matters if it is folder or subdomain.
And this one:
RewriteRule ^blog/* /404.php
But like other, it redirects all to a 404 error, instead of only the direct access to the folder and allowing the access to the subdomain.
Note: I saw other questions that redirects from folder to subdomain, I need to show a 404 error.
Can someone help me?
Thanks in advanced.
Try:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^blog/ /404.php [L,R=404]