ErrorDocument 404 not working with routing - .htaccess

I tried using this code on .htacces to have my own custom 404 on my page,.
ErrorDocument 404 http://mysite.com/404.shtml
when i visit the page that does not exist on the site. it must show something like this
but this is the one it shows
here is my complete htaccess
<files .htaccess>
order allow,deny
deny from all
</files>
php_value memory_limit 170M
RewriteEngine on
Options +FollowSymlinks
Options -Indexes
RewriteCond %{REQUEST_URI} (.+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
AddDefaultCharset utf-8
RewriteRule (.*) index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.mysite.com.*$ [NC]
RewriteRule ^(.*)$ http://mysite.com%{REQUEST_URI} [L,R=301]
ErrorDocument 404 http://mysite.com/404.shtml
The location of my htaccess is located along with the 404.shtml and the index.php of the site
Removing this code would fix the problem. How can i make it work with routing.
RewriteCond %{REQUEST_URI} (.+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
AddDefaultCharset utf-8
RewriteRule (.*) index.php

Instead of referencing the entire url with ErrorDocument 404 http://mysite.com/404.shtml, name the document relative to your web root. In this case, /404.shtml.

Use these codes in your .htaccess for capture 404 error pages.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.html [L]
</IfModule>
Now your all 404 requests will goto /404.html. You can also use /404.php to code more dynamically. Also make sure that rewrite module is enabled in your apache.

Related

Codeigniter V4 not loading public resources

I am using CodeIgniter 4.1.3.
below is my .htaccess
#Options All -Indexes
<IfModule mod_rewrite.c>
#Options +FollowSymlinks
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
ServerSignature Off
options all and followSymlinks not supported on server.
Working fine on localserver
Front page is loaded but it is not loading css,javascript and image from public directory giving 404 error.
not sure what need to be change.
With your shown samples, please try following htaccess Rules file. Make sure to place this in your root directory. Also make sure to clear your browser cache before testing your URLs.
Options -Multiviews
<IfModule mod_rewrite.c>
#Options +FollowSymlinks
RewriteEngine On
RewriteBase /public/
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php/$1 [L,NC,QSA]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
ServerSignature Off
JS/CS rewrite/redirect: You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.
Options All -Indexes
Options +FollowSymlinks
works well on apache but if your server is centos then use this
Options -Indexes
Options +SymLinksIfOwnerMatch

htaccess misconfiguration 500

So basically i created a .htaccess file which removes .html extension and redirects /file.html to /file but when i write /file.html/ it causes a 500 internal server error, How can I map/file.html/ to my custom error page ? or simply redirect that also to /file.html
Here is the code :
Options -Indexes
ErrorDocument 404 /custom404.html
ErrorDocument 403 /custom404.html
ErrorDocument 500 /custom404.html
RewriteEngine on
RewriteCond %{THE_REQUEST} \.html [NC]
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
With your shown samples, could you please try following(comments have been added in rules for explanation). Please make sure to clear your browser cache before testing your URLs.
Options -Indexes
ErrorDocument 404 /custom404.html
ErrorDocument 403 /custom404.html
ErrorDocument 500 /custom404.html
RewriteEngine on
##Added additional condition to make sure this runs from external request only.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/.*\.html\s [NC]
RewriteRule ^(.*)\.html/?$ /$1 [R=301,L,NC]
##Simply rewrite from non-existing pages to html files.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.*)/?$ $1.html [L]

Add a new rule to .htaccess whitout overwrite https server rule

I have a problem when I try to add a new rule to my .htaccess. On my server I activated a rule which redirects http to https and it works fine but when I add a new rule to my .htaccess then https disappears.
My .htaccess
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?$1 [L]
ErrorDocument 404 https://%{HTTP_HOST}/
ErrorDocument 403 https://%{HTTP_HOST}/
</IfModule>
I need to redirect every request to index.php (mvc) and it should work together with https.
I tried deactivating the redirection from the server and to write everything into my .htaccess like this:
Options -Indexes
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?$1 [L]
ErrorDocument 404 https://%{HTTP_HOST}/
ErrorDocument 403 https://%{HTTP_HOST}/
But I get the error The page isn't redirecting properly. How to fix it? Thank you.
I've finally solved it with the help of my internet provider. This is the final code:
RewriteEngine on
RewriteBase /
# force https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} ^www.
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# redirect to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
ErrorDocument 404 https://%{HTTP_HOST}/
ErrorDocument 403 https://%{HTTP_HOST}/

XCart 1.19.2.1 - enabling SSL

Can someone help me with this one please?
We have a XCart site running version 1.19.2.1 and we would like to force all traffic to HTTPS. The SSL certificate is installed and the .htaccess file looks like this:
DirectoryIndex home.php index.php index.html index.htm
# ErrorDocument 404 /404.php
# Show default error document for 404 errors caused by opening of image/media files.
<Files ~ "\.(gif|jpe?g|png|js|css|swf|ico)$">
ErrorDocument 404 default
ErrorDocument 401 default
# Clean URLs [[[
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(ww+\.)?domain\.com
RewriteRule ^(.*) http://www.domain.co.uk/$1 [R=301,L]
<IfModule mod_rewrite.c>
RewriteCond %{ENV:REDIRECT_STATUS} !^401$
RewriteCond %{REQUEST_URI} !^/(payment|admin|provider|partner)/
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png|js|css|swf|php|ico)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ dispatcher.php [L]
</IfModule>
# /Clean URLs ]]]
RewriteEngine on
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php56” package as the default “PHP” programming language.
<IfModule mime_module>
AddType application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
I have tried editing the file to redirect all traffic to HTTPS, I get a too many redirects error.
One example that I tried was replacing the entire contents of the .htaccess file with:
ErrorDocument 404 /index.php
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
rewritecond %{http_host} ^domain.co.uk [nc]
rewriterule ^(.*)$ https://www.domain.co.uk/$1 [r=301,nc]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.co.uk/$1 [R=301,L]
# RewriteRule ^([a-zA-Z0-9\-\%\s\_\.]+)(.htm)$ index.php?page=$1 [L]
##RewriteRule ^([a-zA-Z0-9\-\%\s\_\.]+)\.htm$ index.php?page=$1 [L]
DirectoryIndex index.php
#DirectoryIndex site_closed.html
RewriteEngine on
The above works fine on another XCart site but not this one. Again, I get the too many redirects error.
Anybody have any idea how I can fix it?
Kind regards
James
I had edited the htaccess file but I hadn't edited the https.php file as described here: https://help.x-cart.com/index.php?title=X-Cart:Configuring_HTTPS#Configure_the_HTTPS_server_in_X-Cart
All working now.

mod_rewrite sometimes turns off?

I am using mod_rewrite in my .htaccess files to make cleaner looking URLs. I have two directories (subdir1, subdir2) that are folders created inside the root directory. They are not sub-domains. The .htaccess files reside in the three directories: root, subdir1 and subdir2.
The following .htaccess file resides in the root directory:
Options All -Indexes
ErrorDocument 404 /404.php
FileETag MTime Size
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(subdir1|subdir2)(/.*)?$ [NC]
RewriteCond %{REQUEST_URI} ^(.*)?$ [NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ /subdir1/ [R=301,QSA,L]
RewriteRule ^(.*)(\.html|\.htm)$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php [L]
</IfModule>
and this one is in the subdir1 directory:
Options All -Indexes
ErrorDocument 404 /404.php
FileETag MTime Size
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subdir1/
RewriteRule ^(.*)(\.html|\.htm)$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php [L]
</IfModule>
Everything is working fine. I can browse the www.mydomain.com/subdir1/1234-myaritcle.html page and read its content, but sometimes the rewrite module turns off without creating the clean URLs and the article opens with the following url: www.mydomain.com/index.php?article=1234
Can you please help? What is the cause of the problem?
Is there something wrong with the .htaccess files?
Nothing in your rules is causing the url to appear as www.mydomain.com/index.php?article=1234. Whatever link you are opening the article with is probably not generating the clean URLs.

Resources