I make .htaccess file for my website to send the visitor to the error page If he make an undefined url
The problem is if make the website like this xxxx.com/index.php/something
That doesn't send me to the error page but it's show the index page without images
.htaccess file
ErrorDocument 404 http://xxxxxxxxxxxxx.com/error
This is a known behaviour of Apache. It's got nothing to do with the error document.
Generally, you may be able to reject anything after /index.php by setting the AcceptPathInfo parameter in your .htaccess:
AcceptPathInfo off
But that would probably defeat your purpose, as your index.php file couldn't accept any paths any more and it sounds like that's something it should do.
Likely the best solution for you is to use absolute paths in your images and CSS:
<img src="/images/myimage.gif">
If xxxx.com/index.php/something is an invalid URL, that is something you need to handle inside your PHP application. ErrorDocument won't help you here because it can't know what is an invalid URL for index.php and what isn't.
Set Apache's AcceptPathInfo directive to Off.
Related
I wanted to add a subdirectory to my url so it would become easier to read:
Example of what i'd like:
localhost/testwebsite/users.php?firstname=john
should become
localhost/testwebsite/users/john
I tried using the .htaccess mod_rewrite rules and came up with this:
RewriteEngine On
RewriteBase /testwebsite/
RewriteRule ^users/(.*)$ users.php?firstname=$1
What happens why I use that code: it redirects the page successfully, it shows the html of the correct user and it processes the argument correctly. However, all stylesheets, images, scripts, anything with a relative path, could not be found and respond with a 404 message, because of the extra subdirectory added in the new url, I reckon.
Am I doing something wrong? Is there another technique I should be using? Or should I simply make all paths in my project absolute with regards to the root?
You're doing it right. The browser doesn't know that the actual path of the file is different. Use absolute paths or make paths relative to the easier to read URL.
Our .htaccess file has the usual 404 line, and only one rewrite rule, as below:
ErrorDocument 404 /404.html
# Suppress www. in URLs
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.coastmetalsolutions.com$ [NC]
RewriteRule ^(.*)$ https://coastmetalsolutions.com/$1 [R=301,L]
</IfModule>
If you test page not found with https://coastmetalsolutions.com/test the 404 page works; if you try the same with a / slash on the end, you get a messed up page.
Note:
using Cloudflare
Google Search Console reporting 3 "pages" with redirect errors; all 3 are URLs ending in a / slash and leading to the gibbled 404 page
Oh, heheh, and I just noticed these errors in Ctrl-Shift-I:
Refused to apply style from '' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled
and
GET https://coastmetalsolutions.com/test/js/jquery.js net::ERR_ABORTED 404
There were multiple thing coming from this problem first you are getting 404 and your style sheets were also having 404 error but html 404 page is rendering on there access where else browser is doing strict mime checking.
Frontend
You can use absolute url on your style sheets or try with leading slashes.
like href="https://coastmetalsolutions.com/main.css" or href="/main.css"
And also you can use <base href="/"> tag in your head section you wont have to rewrite every css and js tag.
Server side
Now there is one more thing in server you can comment this section like below.
#Header set X-Content-Type-Options "nosniff"
But I am not suggesting that it will open security loop holes like hotlinking and etc.
Understanding the problem:
An Apache 404 page will respond from multiple URL's. For example, the following are all URL's that could lead to a 404.
https://coastmetalsolutions.com/404
https://coastmetalsolutions.com/404/
https://coastmetalsolutions.com/404/page
https://coastmetalsolutions.com/404/page/
So what happens to your relative paths like <link rel="stylesheet" href="style.css">?
If you are at:
https://coastmetalsolutions.com/404
It will request:
https://coastmetalsolutions.com/style.css
If you are at:
https://coastmetalsolutions.com/404/
It will request:
https://coastmetalsolutions.com/404/style.css
And so-forth.
For this reason, you can't really use relative paths in a 404 page like this.
Option 1 (recommended):
Use more-specific URL's.
You can use a domain-relative URL instead, with a leading slash (/style.css).
<link rel="stylesheet" href="/style.css">
Of-course, a protocol relative (//coastmetalsolutions.com/style.css) and absolute URL (https://coastmetalsolutions.com/style.css) would also work too.
Option 2 (not-recommended):
Sometimes web servers will be configured to redirect error pages to specific URL's to display error page (like: https://coastmetalsolutions.com/404).
However I would not recommend this approach, as it makes more HTTP requests and traffic than necessary.
Mime-type errors:
These are the result of your 404 HTML page being served where CSS and JS are expected. Once you correct those paths or upload those files, those errors should be gone.
What I want to do should be quite simple: when the admin writes www.example.com/admin I want that he's addressed to www.example.com/admin/admin_index.php.
I wrote this rule and I checked on other posts here on Stackoverflow: it apparently seems to be correct, but it actually doesn't work.
RewriteEngine On
RewriteRule ^/admin/?$ /admin/admin_index.php [L,NC]
Has anyone any clue on why the redirect doesn't work, since it "stays" at www.example.com/admin outputting (obviously) the 403 error?
There is no need to use a rewrite rule. Just use DirectoryIndex directive in admin/.htaccess:
DirectoryIndex admin_index.php
This will load admin/admin_index.php when a request comes for http://domain.com/admin/
I already have htaccess redirection for all bad requests to custom/default 404 page:
ErrorDocument 404 /404.html
But still the site can be accessed like:
http://www.domain.com/index.html/http:/domain.com
It cause duplicate content issue and duplicate SEO tag issues.
Any suggestions?
This additional URL on the end of an already valid URL is called "path information" (often passed to your script in the PATH_INFO environment variable). Unless explicitly stated, whether it is accepted or not is controlled by the handler for the request. However, it can be explicitly enabled/disabled with the AcceptPathInfo directive in your .htaccess file:
AcceptPathInfo Off
With it Off, then URLs that contain additional path information will trigger a 404.
I have a simple 404 redirect in a .htaccess file which works fine for Http requests:-
ErrorDocument 404 /apps/handle_error.php
The handle_error.php returns an appropriate PNG image depending on the URL path. However, when I replace the request with Https, it returns the default 404 error page and never does the redirect.
I know my SSL certificate is installed correctly as I get valid content returned for normal static pages. Is there something I am missing in my htaccess file regarding 404 error redirects for Https requests?
(A similar question was asked here but with no clear resolution)
After much soul searching I found the answer, for my specific case anyway.
My Apache2, mod_ssl and mod_rewrite was not configured correctly to work together. The default AllowOverride setting in my SSL config was set to "None" and mod_rewrite requires a minimum of AllowOverride of "FileInfo".
(To be fair, I got my answer from ServerFault)