File htaccees for a 404 page how to set? - php

hi have this file htaccess, i need to improve a 404 page, now when you visit a page that not exist return the index.php content and the url doesn't change, i would like to put a custom 404 , i tried with "ErrorDocument 404" but doesn't work.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^ricercadilavoro.it
RewriteRule ^(.*)$ http://www.ricercadilavoro.it/$1 [R=permanent,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_URI} index\.php$
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L]
rewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L]
</IfModule>

Using the ErrorDocument directive is the correct way to do this. Make sure you're using a path relative to the server root.
ErrorDocument 404 /404.html
For more information, read this http://www.apache.com/docs/httpd-docs-2.2.13.fr/mod/core.html#errordocument.

Related

.htaccess file returning 404 error when redirecting the root folder to a file

i am at my wit's end with this one.
I have a .htacces file which i'm using to redirect certain roots to files.
For example:
example.com/ to example.com/home.php
example.com/subdir/ to example.com/subdir/home.php
This is working great for my subdir redirect but my root redirect to home.php throws up a 404 error and then redirects me to my site's 404 eorr page.
This is my .htaccess file (i've left everything in just incase another rule is throwing things off).
RewriteEngine off
RewriteEngine on
AddDefaultCharset utf-8
#Alter the default time zone for the site
SetEnv TZ Europe/London
#set the RewriteBase
RewriteBase /
#Redirect from root to home.php
RewriteCond %{REQUEST_URI} ^\/?$
RewriteRule (\/)?$ home.php [L]
#Redirect from /subdir to /subdir/home.php
RewriteCond %{REQUEST_URI} ^/subdir\/?$
RewriteRule ^subdir(\/)?$ subdir/home.php [L]
#Removing .php from the end of file paths
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]
#Forcing https://
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI}
#Redirect to 404 error page
ErrorDocument 404 /page_not_found.php
RewriteCond %{HTTP_HOST} ^(www\.)?example.com/(.*)$
RewriteRule ^$ "https://example.com/page_not_found.php" [R=301]
What should i be doing to get example.com and example.com/ to redirect to example.com/home.php?
Try this in after the comments #Removing .php from the end of file paths
Do comment your own code and paste this. It might help you
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Redirecting to specific page on 404 errors if url contains a string in htaccess

I am trying redirect to a specific page (store page) if a user tries to access a page with a url that contains "/product/" as well as gets a 404 error.
This is what I currently have in my htaccess:
RewriteRule ^/product/.* /store [R=404,L]
What do I need to change to get this working?
Here my current htaccess file:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ www.mydomain.com/$1 [R=301,L]
# Send surplus store 404 errors to the store home page
ErrorDocument 404 /store
RewriteEngine On
RewriteRule ^/?product(/.*)?$ - [R=404,L,NC]
# handle both specific URL redirects for woocommerce
RewriteRule ^/?product-(?:tag|categories|category)/([\w-]+) www.mydomain.com/parts?category=$1 [R=301,L,NC,QSA]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
RewriteRule ^MyAccount\.aspx\/?(.*)$ "http\:\/\/online\.mydomain\.com\/$1" [R=301,L]
You can use this code in your site root .htaccess:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
# handle both specific URL redirects for woocommerce
RewriteRule ^/?product-(?:tag|categories|category)/([\w-]+) www.mydomain.com/parts?category=$1 [R=301,L,NC,QSA,NE]
RewriteCond %{HTTP_HOST} ^(?:www\.)?mydomain\.com$ [NC]
RewriteRule ^MyAccount\.aspx(/.*)?$ http://online.mydomain.com$1 [R=301,L,NC,NE]
# redirect /product URIs
RewriteRule ^/?product(/.*)?$ /store [R=301,L,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

.htaccess broke my ajax

I tried a .htaccess to remove the index.php part of the url, and now the ajax content won’t load anymore, even if I remove the .htaccess. Here’s what the .htaccess looks like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
The error Chrome is giving me looks like this:
Failed to load resource: the server responded with a status of 404 (Not Found)
http://jolinmasson.com/ajax/page:6?_=1397504793977
Finally managed to get it working with that .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^content/(.*)\.(txt|md|mdown)$ error [R=301,L]
RewriteRule ^site/(.*) error [R=301,L]
RewriteRule ^kirby/(.*) error [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]
</IfModule>

remove index.php and 301 redirect with codeigniter and htaccess

I have 5 domains all pointing to the same server, and for SEO purposes I have setup an canonical meta link specifying the main domain.
My question is, how do I remove the index.php from the url and also 301 redirect all my domains to the main domain?
Here is my .htaccess to remove the index.php
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|sitemap\.xml)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
ErrorDocument 404 /index.php
Here is my .htaccess to 301 redirect
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_NAME} !^www.example.com$
RewriteRule .* http://www.example.com%{REQUEST_URI} [R=301,L]
I tried to merge the two but then I got some weird results, it would redirect the domain, but place the entire url as a paramter of the page...
You need the redirect to happen before the routing rule (the one that sends everything to /index.php:
RewriteEngine On
RewriteCond %{SERVER_NAME} !^www.example.com$
RewriteRule .* http://www.example.com%{REQUEST_URI} [R=301,L]
RewriteCond $1 !^(index\.php|robots\.txt|sitemap\.xml)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
ErrorDocument 404 /index.php
DirectoryIndex index.php

VHost & htaccess - 500 Internal Server error

I use ISPConfig 3 to manage my domains/subdomains.
I added subdomain parim.kristian.ee which would redirect to web/parim/ (note: web/ is my document root).
ISPConfig generated such redirect to apache config:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^parim.kristian.ee [NC]
RewriteRule ^/(.*)$ /parim/$1 [L]
Now if i'll try to get static resources, such as http://parim.kristian.ee/images/1x1.gif, it serves fine, but when redirect is to codeigniter, it doesn't work.
Htaccess in web/parim/ looks like this:
<IfModule mod_rewrite.c>
RewriteEngine on
Options -Indexes
#Handle CodeIgniter request
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 ./index.php
</IfModule>
NOTE: accessing the same folder via http://kristian.ee/parim/ works!
I s*ck at htaccess, so any help is appreciated.
# To remove index.php from URL
RewriteEngine On
RewriteBase /parim
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Or Simple remove RewriteBase /parim

Categories