htaccess physcially redirect in case of 404 - .htaccess

To start off I know how to do a simple redirect to a 404 when a page doesn't exist with.
ErrorDocument 404 /index.php
Is what I want to do is a physical redirect to the index page. So if a user types in mydomain.com/abc and that page doesn't exist it does an actual redirect TO the index page. I don't want the url to remain the same with the bad address. Yes I know its not a big deal but it bugs me. I could accomplish this by having a custom 404 page redirect to the homepage but I don't want this.
I've tried this with the [R] redirect flag and obviously it doesn't work.
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^(.*)$ /index.php [L,R]
Update
This is the error I get it rewrites the url to mydomain.com/index.php
I tried to remove the index.php with just / and it didn't redirect but gave the same error below.
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept
cookies.

Do this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !(?:\.\w+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([^/]+)/?$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,R]
Explanation:
checks whether the file has extension:
RewriteCond %{REQUEST_URI} !(?:\.\w+)$ [NC]
If not, checks whether file is present:
RewriteCond %{REQUEST_FILENAME} !-f
If not a file, checks whether it is a folder which is present:
RewriteCond %{REQUEST_FILENAME} !-d
If not append .php. If / is present at the end, remove it and append .php
RewriteRule ([^/]+)/?$ $1.php
Checks whether the .php appended or whatever extension file is actually a file which is present:
RewriteCond %{REQUEST_FILENAME} !-f
check whether it is a directory:
RewriteCond %{REQUEST_FILENAME} !-d
If not, then it is a invalid file request and redirects it to /
RewriteRule ^(.*)$ / [L,R]

This should be your mod_rewrite based 404 handling:
# if it is not a file, directory or link
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php [L,R]
However I am not sure what are you doing in your first rule? Are you trying to add .php to each request URI?

Related

htaccess rewrite going to 404?

for the last hours I cant seem to figure out why my .htaccess file is redirecting to a blank url. Here is my htaccess file below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ /test/user-profile/%1? [R=301,L]
I am trying to rewrite www.example/test/user-profile?id=4 ->>> www.example/test/user-profile/4
It does rewrite the url, however then the page has "The requested URL was not found on this server." the htaccess file is in public_html folder, so I am trying to select a specific user inside www.exampleurl.com/test/userslist.php which would go to www.exampleurl.com/test/user-profile.php. It all works perfectly before using htaccess. www.example/test/user-profile?id=4 ->>> www.example/test/user-profile/4 but it just wont find the url or file? im a super noob folder path below -> public_html/test/userslist.php & user-profile.php
Just cant seem to figure out what to do. Yes I do have mod-rewrite on.
With your shown samples, please try following htaccess rules file.
This assumes that you are hitting link www.example/test/user-profile?id=4 in browser which is redirecting to www.example/test/user-profile/4 and is being served by index.php with parameters user-profile=user-profile in your url and id=digits in url. You can also change them as per your need.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /test/
RewriteCond %{THE_REQUEST} \s/(test/user-profile)\?id=(\d+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*/)/([*/]*)/?$ test/user-profile.php?user-profile=$1&id=$2 [QSA,L]
2nd solution: In case someone is hitting url example.com/test/user-profile/7 then try following htaccess rules.
RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*/)/([^/]*)/(\d+)/?$ test/user-profile.php?user-profile=$1&id=$2 [QSA,L]

Redirect unavailable pages to 404 without changing URL

I have an hidden folder hidden_folder which can be accessed by the server with
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ hidden_folder/$1
Now, I want to redirect all unavailable pages to 404 error page content.
Illustration 1 : When user tries to go here http://website.com/some_unavailable_uri, the url of the page doesn't change but the content will be the one of /error/404.php
Illustration 2 : http://www.apple.com/test. This page does not exist, the url does not change, but the content displays error.
Basically, it should be if file does not exist, then redirect content. I'd tried something but with no success.
First try :
RewriteEngine On
RewriteCond hidden_folder%{REQUEST_URI} -f # %{REQUEST_URI} already has "/" at its beginning
RewriteRule ^.*$ error/404.php [NC,L]
Second try :
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}hidden_folder%{REQUEST_URI} -f
RewriteRule ^.*$ error/404.php [NC,L]
Any idea ?
Looks like you just need a simple ErrorDocument with use of http:// in target since that causes URL to change in the browser.
Use this line top of your .htaccess:
ErrorDocument 404 /error/404.php
And don't forget to comment out your rewrite rules doing same.
The conditions are tested also on redirects, so you must make sure that the 404 redirect doesn't catch more than it should.
What this means is that %{REQUEST_URI} will contain hidden_folder once it's been redirected, so you must add a RewriteCond to avoid it.
This should work:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/hidden_folder%{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !^/hidden_folder
RewriteRule ^(.*)$ error/404.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ hidden_folder/$1 [L]
Thank you Outlyer.
The actual working code is :
RewriteEngine On
# If the requested file is not in hidden_folder as a file
RewriteCond %{DOCUMENT_ROOT}hidden_folder%{REQUEST_URI} !-f
# AND
# If the requested file not is in hidden_folder as a folder
RewriteCond %{DOCUMENT_ROOT}hidden_folder%{REQUEST_URI} !-d
# AND
# If the requested file is not in root as a file
RewriteCond %{REQUEST_FILENAME} !-f
# AND
# If the requested file is not in root as a folder
RewriteCond %{REQUEST_FILENAME} !-d
# AND
# ???
RewriteCond %{REQUEST_URI} !^/hidden_folder
# THEN Redirect content
RewriteRule ^(.*)$ error/404.php [L]
####################################################
# Enable the deletion of hidden_folder in urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ hidden_folder/$1 [L]

Redirect all requests to index.php and clean the URL

I was wondering if it's possible to redirect all requests to files/directories that do not exist to index.php, and after that clean the url.
So when I go to www.example.com/test/1 I want it to be redirected (rewrote?) to www.example.com
I tried the following and it does correctly load index.php, but it still says www.example.com/test/1 in the URL bar...
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
Of course I still want my css and js to load properly...
Try this. If I understand, you want to use this as a way of redirecting a 404 not found?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [R=301,L]
Or maybe this without index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [R=301,L]

htaccess is not ignoring images like it should?

I have an htaccess file that is supposed to be redirecting any non-existing files/folders/etc into the application's index.php script where they are handled by the application's SEO rewriting.
The problem is that any missing images are also redirecting into the application which is causing a lot of additional load and is unnecessary. I've been attempting to get this htaccess file to ignore images, but even though this should be working, it's not, and I'm completely out of ideas as to why...
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !.*\.(gif|png|jpe?g|ico|swf)$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?sef_rewrite=1 [L]
When I make a request to http://www.domain.com/folder_that_doesnt_exist/image.jpg this redirects to index.php
When I make a request to http://www.domain.com/folder_that_does_exist/image.jpg it also redirects to the index script
I'm not sure what I'm missing here because unless OR is specified, shouldn't the RewriteRule only be applied if the request passes all of the RewriteCond statements? which it clearly should not be passing...
Update:
I've modified the code to the following just to eliminate possible issues, but it still redirects all non-existing images into the script...
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(gif|png|jpe?g|ico|swf)$ [NC]
RewriteCond %{REQUEST_FILENAME} !\.(gif|png|jpe?g|ico|swf)$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?sef_rewrite=1 [L]
Maby try this:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} \.(gif|png|jpe?g|ico|swf)$ [NC]
RewriteRule .* - [R=404,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]

apache mod-rewrite

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([0-9a-zA-Z_\-]+)(|\.html|\.php)$ page.php
this is the rule is .htacess.
what I want is if file is not found then redirect to page.php.
for example if url people.php ,people.html is not found then redirect to page.php.
because I also want those filenames without .php like people direct to people.php first. then check file exist or not, if not redirect to page.php.
how do I add one rule to make people redirect to people.php
These rules should work for you:
RewriteEngine on
Options +FollowSymlinks -MultiViews
# if there is no . in request then append .php
# it is important to make [L] as last rule here
RewriteCond %{REQUEST_FILENAME} !\.
RewriteRule ^(.+)$ /$1.php [L]
# 404 handling for files that don't exist
# NC is for ignore case comparison
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^.]+\.(html|php)$ /page.php [L,NC]
See if this does what you're looking for:
RewriteEngine on
RewriteRule ^(?:.+/)?([^/.]+)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \.(?:html|php)$ page.php

Resources