.htaccess - how to use ErrorDocument 404 without 404 in header? - .htaccess

I have some files in /templates/ . Suppose I get an request /templates/something.html. I want to check if the actual file /templates/something.html exists and print it. Otherwise I want my script /search_here.php to do some work and print special output.
I'm using the below in my htaccess file. It works almost fine, the only problem is that in case search_here.php do the work I see a good output, but server sends 404 header (HTTP/1.1 404 Not Found). How can I turn off this header (my /search_here.php will print this header in case of need)?
Any help would be appreciated.
RewriteEngine on
Options +FollowSymlinks
#RewriteBase /
RewriteCond %{REQUEST_URI} !/templates/
RewriteRule ^.*$ index.php [L]
ErrorDocument 404 /search_here.php

You should not use /search_here.php as 404 handler.
You can use:
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule !^templates/ index.php [L,NC]
RewriteRule ^ search_here.php [L]

Related

.htaccess Pages open even though the URL is broken

I have a problem with my website opening pages that should show 404 error. It seems that only the first part of the URL is taken into account.
Here's an example
example.com/page.php - (opens - correct)
example.com/asdasd.php - (error 404 - correct)
example.com/asdasd.php/asdasd - (error 404 - correct)
example.com/page.php/asdasd - (opens - incorrect)
example.com/page/ - (opens - incorrect)
example.com/page/asdasd (opens - incorrect)
I would like to get an Error 404 and if I feel that the URL is important also be able to Redirect it. Now I can't even Redirect URLs that for example look like this "example.com/page.php/asdasd"
The closest answer that I found was this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=302,L]
RewriteCond %{THE_REQUEST} ^(?:GET|HEAD)\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+?)/?$ $1.php [L]
But the problem was that I got something like this:
Input URL
example.com/page.php/asdasd
Output URL
example.com/page
I need it for the .php extention to stay. And if possible instead of "Redirecting" to the example.com/page.php, I'd like to get an Error 404
This is what I currently have in the htaccess file:
ErrorDocument 404 /404.php
ErrorDocument 401 /404.php
ErrorDocument 403 /404.php
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]
RewriteRule ^index\.php$ / [R=301,L]
Sorry if this is a duplicate but I don't even know what I should look for.
example.com/page.php/asdasd - (opens - incorrect)
This is due to a feature called Path Info - if Apache manages to match the first part of the requested URL to an existing file, then it will serve that file, and pass the rest of the path along via environment variables (so that a script could in theory make use of this information.)
The AcceptPathInfo directive makes it possible to disable this behavior.
https://httpd.apache.org/docs/2.4/mod/core.html#acceptpathinfo

URL rewrite for language query variable to fake directory conversion returns 404 error

I have surely read and tested all the solutions found on Stackoverflow but couldn't make it work ... so here I am.
I'm trying to transform
example.com/?l=en
into
example.com/en/
( /en/ isn't supposed to exist on the server )
I use the following htaccess
DirectoryIndex index.php
ErrorDocument 404 https://example.com/
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
RewriteCond %{QUERY_STRING} (^|&)l=en($|&)
RewriteRule ^$ /en/?&%{QUERY_STRING}
RewriteCond %{QUERY_STRING} (^|&)l=ja($|&)
RewriteRule ^$ /ja/?&%{QUERY_STRING}
Since the last line triggers a 404 error, the errorDocument 404 kicks in.
The rule returns a "page not found" in the browser when I comment the errorDocument line.

htaccess 404 page for specific subdirectory only (static html pages)

Stuck again with htaccess. How can I add a 404 page to a specific subdirectory?
In my case the 404 page should only affect the "staging" subdirectory (and the directories inside of staging) but not the root or any other subdirectories placed in root.
http://example.com/staging
The code which is in my .htaccess file for the directory "staging":
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
Options -Indexes -MultiViews
RewriteEngine on
RewriteRule ^arbeiten/$ /staging/arbeiten/alle [R=301,L]
RewriteRule ^journal/$ /staging/journal/journal-alle [R=301,L]
ErrorDocument 404 /staging/404.html
Using the code above (and many others) results in a Internal Server Error.
Update #1 to my initial question.
In general, the code/answer provided to add a 404 not found page to a specific sub directory works:
ErrorDocument 404 /staging/404.html
The reason why it still does not work is because there must be some error in the overall .htaccess setup related to the code remove .html extension and/or redirect to a page in a folder.
Options -Indexes -MultiViews
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
RewriteRule ^arbeiten/$ /staging/arbeiten/alle [R=301,L]
RewriteRule ^journal/$ /staging/journal/journal-alle [R=301,L]
ErrorDocument 404 /at/404.html
I will continue to further investigate the setup in my .htaccess.
Update #2
Using the .htaccess script below allows to remove .html extensions, redirect pages to a specific page within a directory and basically a working 404 not found page within a specific sub directory on a Hostgator cPanel grid hosting (apache).
Options -Indexes -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
RewriteRule ^arbeiten/$ /staging/arbeiten/alle [R=301,L]
RewriteRule ^journal/$ /staging/journal/journal-alle [R=301,L]
ErrorDocument 404 http://www.example.com/staging/404
Remaining 404 problem:
http://example.com/staging/blabla (404 works)
http://example.com/staging/folder/blala (404 works)
http://example.com/staging/folder/project-detail-pagexxx (404 works)
http://example.com/staging/folder/project-detail-pagexxx/ (404 does not work)

How do I redirect nonexistent pages to the 404 error page with .htaccess?

Apparently Bingbot is getting caught in an infinite loop on my site. It downloads pages like http://www.htmlcodetutorial.com/quicklist.html/applets/applets/applets/applets/applets/applets/applets/applets/applets/applets/applets/applets/applets/applets/sounds/forms/linking/frames/document/linking/images/_AREA_onMouseOver.html . Since I set my server to interpret .html as PHP the page is simply a copy of http://www.htmlcodetutorial.com/quicklist.html . How do I stop Bingbot from looking for these bogus copies?
Why is Bingbot looking for those pages to begin with?
I'd like to do something like the last line of the .htaccess file shown below (like at "Redirect to Apache built-in 404 page with mod_rewrite?"), but when I try RewriteRule ^.*\.html\/.*$ - [R=404] the entire site shows a 500 error.
Even when I use the last line below it redirects to http://www.htmlcodetutorial.com/home/htmlcode/public_html/help.html which is not what I wanted.
AddType application/x-httpd-php .php .html
RewriteEngine on
Options +FollowSymlinks
RewriteRule ^help\/.* help.html [L]
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.htmlcodetutorial.com/$1 [R=301,L]
ErrorDocument 404 /404.html
RewriteRule ^.*\.html\/.*$ help.html [R=301]
P.S. I know the site is way out of date.
The problem here is that you either have Multiviews turned on, or apache is interpreting requests like /quicklist.html/blah/blah as a PATH_INFO style request, which will be interpreted as a valid request.
So turn off multiviews by changing your options line to:
Options +FollowSymlinks -Multiviews
Then replace your last rule with:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^ - [L,R=404]
Change your last rule to this:
RewriteRule ^(.+?\.html)/.+$ - [R=404,L,NC]

htaccess ErrorDocument 404 conflict with RewriteEngine On

I'm using the below in my htaccess file to strip out the .php extensions on my files. It works fine.
However, I also want to utilize ErrorDocument 404 /index.php to redirect any non existing urls back to the homepage. If I use ErrorDocument 404 /index.php alone in the htaccess, the redirects work fine. Also, if I use the below Rewrite code it successfully strips the php extensions. I would like both of these to work together, however.
For example, if a user enters "www.domain.com/register", they go to www.domain.com/register.php. But if they enter "www.domain.com/something", and something.php doesn't exist, then they redirect back to index.php. I've tried every permutation of both of these together in my htaccess to no avail. Any help would be appreciated.
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
Not sure what the problem you're having is exactly, but if I change your rules to this, it works for me:
ErrorDocument 404 /index.php
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
So instead of checking if the request isn't accessing an actual file/directory that exists, check that the .php version of that file DOES exist.

Resources