htaccess 404 redirect not working - .htaccess

I am trying to setup a custom 404 page for my website using: ErrorDocument 404 /404.html but when I add this, going to domain.com/dslkjflsdkjflskdjf doesn't redirect to the 404 page. Instead, it redirects to my homepage where mostly everything on the page is broken.
I'm wondering if this mod_rewrite.c module is the cause of this, I've tried modifying it (taking L out, adding the redirect inside the module, etc) but no luck.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>

I'm not sure about this but I think your server won't recognize a 404 error because every request is redirected to your index.php. You have to determine in the index.php if there is a 404 error and send the header with the header() function.

Related

htaccess not redirecting all not found page or URL on site to 404 pages

Try to set 404 page not found error page by htaccess.
Issue is if we are searching for
https://www.rsseosolution.com/suraj.html ... Its redirect perfect to 404.php.
But if we are searching for
https://www.rsseosolution.com/suraj.php ... Its not redirecting to 404.php and giving showing simple text message "File not found.".
In short problem is if extension is not php then its redirecting fine but if .php then its showing File not found.
ErrorDocument 404 https://www.rsseosolution.com/404.php
Options +FollowSymLinks
RewriteEngine on
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule seo-package-(.*)-n-(.*)\.php$ seo-package-detail.php?id=$1&name=$2 [NC,L]
RewriteRule case-studies/(.*)-(.*)\.php$ case-studies.php?project=$1&id=$2 [NC,L]
RewriteRule tutorial/(.*)-(.*)\.php$ tutorial.php?topic=$1&id=$2 [NC,L]
RewriteRule blog-page-(.*)\.php$ blog.php?page=$1 [NC,L]
RewriteRule seo-tutorial-(.*)\.php$ seo-tutorial.php?page=$1 [NC,L]
RewriteRule frequently-ask-question-faq-(.*)\.php$ frequently-ask-question-faq.php?page=$1 [NC,L]
What i am looking for is ....... redirect all not found or wrong URL to 404.php.
What i am missing here.
I think because we are rewriting some URLs with .php extension from htaccess and thats why for PHP its saying file not found but for other its redirecting perfectly to 404.php.
Is there any ways to set 404.php for all not found pages and URL without change any extension (.php) of previous written file by htaccess.
The problem is not to do with your current directives in .htaccess.
The problem is mostly likely due to your server config and the way PHP is implemented on your server. If requests for .php files are proxied to a backend server for processing as is often the case with FastCGI type configs then this 404 Not Found message is likely coming from the backend server and not your Apache server.
Ordinarily, you would solve this with the ProxyErrorOverride directive in the reverse proxy config:
ProxyErrorOverride On
If you don't have access to the main server config then you may be able to trigger the 404 early using mod_rewrite in .htaccess, before the request is sent to the proxy server.
For example, before your existing mod_rewrite directives:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.php$ - [R=404]
However, this is strictly a "workaround", if you have access to the server config then using ProxyErrorOverride (as mentioned above) is preferable.
Aside:
ErrorDocument 404 https://www.rsseosolution.com/404.php
By specifying an absolute URL in the ErrorDocument directive this triggers an external (302) redirect for the error document (an additional request), which is generally undesirable (and you need to manually set the 404 response code). It is far better to issue the error document as an internal subrequest instead:
ErrorDocument 404 /404.php

.htaccess extensionless URLs + ErrorDocument 404 issue

I use the following code to serve extensionless URLS. By that I mean anyone requesting myfile.php will be served myfile
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I also use the following so I can use a custom 404 page:
ErrorDocument 404 /error.php
All that works fine until someone types in an extentionless URL which doesn't exist, such as "/abcdefghijkl" In this case Apache returns a blank page with "File not found" which isn't very helpful to users! In such a case, I want the error page (error.php) to be displayed. Is it possible?

How to handle 403 errors without redirecting to another page

My htaccess file currently handles 404 errors as following:
Options +MultiViews
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ 404.html [L]
I did not use the ErrorDocument, because it redirects to an absolute error path.
I want to stay at the same url but show a custom template for a 403. Currently the 403 shows the default error page of apache... I would like to show a custom error document here too.
How would I do this?
Thanks for your help ;)
You can use ErrorDocument with a relative target path . With a relative path ErrorDocument directive won't redirect your request but instead show the error page without changing the url.
ErrorDocument 403 /403.php

different 404 for subdirectory when paths are handled by index.php

I'd like /blog/anything-that-doesn't-exist to redirect to /blog, while any other 404s are handled by Drupal.
My htaccess currently has:
ErrorDocument 404 /index.php
So everything is handled by Drupal. Is it possible to have a subdirectory specific 404 rule, but set that from the root directory htaccess? The /blog/ subdirectory is generated by Drupal, so I can't put an overriding htaccess in there.
You can use the following code in your Root/.htaccess file :
RewriteEngine On
#If /blog/foo is not a directory
RewriteCond %{DOCUMENT_ROOT}/blog/$1 !-d
#And /blog/foo is not a file
RewriteCond %{DOCUMENT_ROOT}/blog/$1 !-f
#Then redirect /blog/foo to /blog
RewriteRule ^blog/(.+)$ /blog/ [NC,L,R]
It is worth noting that, the Execution time of RewriteRule and ErrorDocument directive is diffrent. RewriteRule directive executes before the ErrorDocument directive, so any request for /blog/bad_url will be handled by the rule above, and all other 404 requests will be handled by the ErrorDocument.

Codeigniter Index.php in HTTPS URL

I'm having an issue with CodeIgniter and htaccess.
When I access to my website in http or in localhost, I can access to mysite.ext/controller/view , like admin/login, for example.
But when I access to my website with HTTPS before, it's sending a 404 error, so I have to write mysite.ext/index.php/controller/view, and it works.
The problem is I don't want the index.php and when I try solutions I found on the internet, it removes index.php sometime, but I can only access to the /index/ controller, not the admin and the other ones.
I tried this solution here :
codeigniter + not index.php with https
It removes the index.php, which is the bootstrap, and the controller/view index/firstpage works, but not the other controllers.
PS : the HTTPS URL is different than the HTTP URL. HTTP URL takes to the server, but the HTTPS is provided by my hoster (so, https:// ovh....blabla../mywebsite/)
I would like to understand why it's not working and find how to correct it.
There is nothing specailly do configuration for codeIgniter setup in https url. First you remove $config['base_url] from your config file and put ''(blank) in your $config['index'] attribute ..
you can set your .htaccess file like..
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
Above code is remove index.php from url.

Resources