Creating a re-direct for 404 pages - php

I created a 404.php page and added the following in my .htaccess file
ErrorDocument 404 /404.php
Also played around with removing the slash, adding a full URL, etc,
No matter what I do, I get my index.php UI regardless to what I write in the URL. Here is the thing, IT IS NOT re-directing me to domain.com/index.php or "/". The URL remains, but the UI is the index.php content
I'm adding my re-direct below in case you see something there that is conflicting
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

From what I know of the mod_rewrite the last 4 lines are saying
That if the request filename is not a file nor a directory,
redirect to index.php.
And this is what is happening I believe.
All of the routes that are not available on the physical path in your application are going to index.php.
You will need some 404 mechanism in your application. If you are using some framework it usually has an exception like "RouteNotFound" thrown and then error handler redirects to a 404 page, you can also do something similar and redirect from within your application to 404.php.
if($should_be_an_error) {
header("Location: 404.php");
}
(haven't used something like this for years but should be something similar)
or remove
`
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
`
But then your other routes might stop working.
EDIT:
Since you wanted to redirect to 404.php instead of index.php(getting that from comments) the code should be changed to
RewriteRule . /404.php [L]
(last line of the rewrite block)

You can do this using FallbackResource directive. Remove all the code starting with RewriteBase / line and use this single line:
FallbackResource /404.php
This will fallback to /404.php for any 404 event.

You can see this code below as an example if it match your requirement:
ErrorDocument 404 http://example.com/404/
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /404.php [L]

Related

404 Error not displaying as it should

I have a project at hand but encountered an error.
I have RewriteRule to redirect index.php?view=$1 to /. However, when I accessed a URL that is supposed to lead to a 404 Error, it displays the home page(index.php) instead.
ErrorDocument 404 /pages/errors/error_redirect.php
ErrorDocument 500 /pages/errors/error_redirect.php
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
<FilesMatch "\.tpl$">
Order Allow,Deny
Deny from all
</FilesMatch>
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /pages/errors/404.php [L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]
RewriteCond %{REQUEST_FILENAME} !-l
# Rewrite all URLs to non-extension URLs
RewriteRule ^(admin|user)($|/) - [L]
RewriteRule ^([A-Za-z0-9-_]+)$ /index.php?view=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
The problem I am facing is that if I entered http://localhost/asdasdads, the browser led me to http://localhost/ instead with unchanged URL in the browser but browser displaying index.php. It should have shown a 404 Error page.
I believe RewriteRule ^([A-Za-z0-9-_]+)$ /index.php?view=$1 [L] is the root of this problem. It is redirecting /index.php?view=asdasdads to /index.php?view= due to it is not found. I would want it to redirect to 404 instead.
Thank you in advance.
Aizat, try removing the unnecessary rewrite conditions, and give a try on this:
The simplest way to set a 404 error page is by directly setting a 404 error message in the .htaccess file itself:
ErrorDocument 404 /404.php

htaccess 404 redirect does not working

I have a .htaccess file to manage rewrite_rules on my website. I also set a 404 redirect command to navigate visitors to a specefied page if they enter a wrong URL. But this redirect does not working and I get 500 Internal Server Error if visitor request an invalid URL. Below is some part of my codes:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/?$ index.php?lang=$1&page=$2&id=$3&des=$4 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/(p[0-9]+)/?$ index.php?lang=$1&page=$2&pn=$3 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&page=$2 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?lang=$1&page=home [NC,QSA,L]
ErrorDocument 404 http://www.mywebsite.com/404/
What is missing in my code that causes this problem?
The culprit, by seeing all of your .htaccess rules is:
RewriteRule ^(.*)$ $1 [NS,E=no-gzip:1,E=dont-vary:1]
Giving your sample URL http://www.domain.com/en/news/1/something/test/, when a URL does not match with defined rules it hits this last rewrite rule and it falls into an infinite loop:
applying pattern '^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/?$' to uri 'en/news/1/something/test/'
...
applying pattern '^([^/]+)/([^/]+)/(p[0-9]+)/?$' to uri 'en/news/1/something/test/'
...
applying pattern '^([^/]+)/([^/]+)/?$' to uri 'en/news/1/something/test/'
...
applying pattern '^([^/]+)/?$' to uri 'en/news/1/something/test/'
...
applying pattern '^(.*)$' to uri 'en/news/1/something/test/'
...
Then we have a recursion start:
applying pattern '^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/?$' to uri 'en/news/1/something/test/'
...
Although a URL like http://www.domain.com/en/news/1/wrong is not found, it's caught by a defined rule (first rule) and we don't encounter any recursion. So this internal error is not thrown on all not found pages but those that are not caught by a rule.
I don't see it right, just remove it.
you should put
<IfModule mod_rewrite.c>
RewriteEngine On
in the start and
</IfModule>
in the end of your .htaccess code
Try adding this rule to the top of your htaccess:
RewriteEngine On
RewriteRule ^404/?$ /pages/errors/404.php [L]
Then under that :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ http://domain.com/404/ [L,R]
You can use an EroorDocument directive to forword non existent requests to a specific destination
Test this in an empty .htaccess file
ErrorDocument 404 /page.php
Check below code, I have write with various scenario.
ErrorDocument 404 http://example.com/404/
ErrorDocument 500 http://example.com/500/
# or map them to one error document:
# ErrorDocument 404 /pages/errors/error_redirect.php
# ErrorDocument 500 /pages/errors/error_redirect.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /pages/errors/404.php [L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]
# or map them to one error document:
#RewriteCond %{REQUEST_URI} ^/404/$ [OR]
#RewriteCond %{REQUEST_URI} ^/500/$
#RewriteRule ^(.*)$ /pages/errors/error_redirect.php [L]
It seems your mentioned URL after ErrorDocument is not valid or is not reachable.
So, what happening is that when it gets a 404 it redirects to your URL, and it gets a 404 again.
Forms a recursive loop.
Getting my point. ?
Try with a simple static html page.
I hope this shall work.
Custom 404 will never work if you add domain like http://www.mywebsite.com/404/ (it's not coding karma), when you type url like this it will ended up in 500 error. It means it executed 404 but redirected user to 500 error due to wrongly configured htaccess.
When error comes in series of 4XX are knows as client side errors and 5XX known as server side errors. For detailed info, 2XX known as Success & 3XX known as Redirection.
So here is quick try (i assume you have 404 html file)
ErrorDocument 404 /404.html
instead of
ErrorDocument 404 http://www.mywebsite.com/404/
I also have tested this code at my side and it worked. Please try out
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/([^/]+)/?$ index.php?lang=$1&page=$2&id=$3&des=$4 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/(p[0-9]+)/?$ index.php?lang=$1&page=$2&pn=$3 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&page=$2 [NC,QSA,L]
ErrorDocument 404 /404.html
Try this one:
ErrorDocument 404 /404/
Try adding before rewriterules
RewriteEngine On

.htaccess redirect attempt redirects wrong domain to wrong url

I went and broke my .htaccess somehow. I admit that .htaccess is not something I usually mess around with, but I thought if I followed other examples, I'd manage it.
Originally I was trying to redirect www.shadowlordscomics.com to a new drupal site in a subfolder. But I left off the s at the end of comics, which broke the url. However, instead of redirecting www.shadowlordscomics.com to the broken url, it redirected a different domain that uses the same space, officialshadowlords.com.
The .htaccess file originally only had this inside of it:
Options -Indexes
What I used for my redirect was based off of this:
Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteRule .* http://www.mysite.com/ [L,R=301]
RewriteRule ^$ drupal/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]
Is there a way to revert the redirect? To make it stop redirecting to the broken url? Officialshadowlords.com is meant to be going to an index.html file in its sub-folder.
you can redirect to home page using errordocument
ERRORDOCUMENT 404 /

htaccess two commands in the same file

Hi i have my htaccess with this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.htm [L]
ErrorDocument 404 /404.htm
if i erase the first 4 lines, the other piece of code (which redirects de 404 page when there's a page not found) does not work anymore.
Any ideas?
I also want that when it redirects to 404.htm, it does not show on the url box, this url
http://www.mypage.com/404.htm
i want to show this
http://www.mypage.com/fail/search-by-the-user
for example.
Try following .htaccess
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ /$1.html
ErrorDocument 404 /404.html
NOTE: if you are working at localhost then please mention proper RewriteBase

.htaccess redirect everything except certain request

I am having an issue with my .htaccess file. It is redirecting everything through my index.php page which is what I want for the majority of requests.
BUT the only time I do not want to redirect is via an AJAX call.
It redirects any request through my index.php file like so:
RewriteRule /.* /index.php [NC,L]
The AJAX request url is:
http://myurl.dev/login/ajax/functions.php
With the directory structure:
/modules/login/ajax/functions.php
I am inexperienced with regex and RewriteRules, and have read / tried many variations with varying logic but cannot stop anything from /ajax/ to not redirect to the index page.
I have tried a RewriteCond before the Index RewriteRule to redirect to index unless /ajax/ but no luck.
Rewrite Cond %{REQUEST_URI} !(.*)/ajax
RewriteRule /.* /index.php [NC,L]
Also tried a seperate RewriteRule for the /ajax/ request:
RewriteRule ^(.*)/ajax/functions\.php$ /modules/$1/ajax/functions.php [NC,L]
So nothing as worked so far, it either redirects to the index or hits a 500 server error.
Does anybody have any suggestions or links to help? Thanks.
Note: When I say redirect, I do not mean a full page refresh as I know that Apache wont do a full url refresh without the [R] flag.
-- Edit: Working file --
Here is my full .htaccess code:
Options +FollowSymLinks
RewriteEngine on
# Intercept any requests to the core, and keep them being written to the core (so they don't go to index.php)
RewriteRule ^core/(.*)$ core/$1 [NC,L]
# The magic, stops any requests to a file for being redirected.
# needed to be under the /core/ redirect
RewriteCond %{SCRIPT_FILENAME} !-f
# Rewrite all requests to index.php.
RewriteRule /.* /index.php [NC,L]
# Some requests (without trailing slashes) can fall through the above rule. This bit catches those stragglers.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
ErrorDocument 404 /404/
Use
RewriteCond %{SCRIPT_FILENAME} !-d //excludes existing directories
RewriteCond %{SCRIPT_FILENAME} !-f //excludes existing files
before any RewriteRule . This will exclude any directory or file which already exists, so the RewriteRule won't work on http://myurl.dev/login/ajax/functions.php, because it actually exists, but it will work on http://myurl.dev/someOtherNonExistantFile.php
That makes this your full .htaccess file code:
AuthType Basic
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
# Intercept any requests to the core, and keep them being written to the core (so they don't go to index.php)
RewriteRule ^core/(.*)$ core/$1 [NC,L]
# Rewrite all requests to index.php.
RewriteRule /.* /index.php [NC,L]
# Some requests (without trailing slashes) can fall through the above rule. This bit catches those stragglers.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
ErrorDocument 404 /404/

Categories