NoT found url to show contents of index.php - apache

Hello i need to show the content of index.php for all the not found url,
For example
http://domain.com/random must show http://domain.com/index.php content
http://domain.com/random/random.html must show http://domain.com/index.php content
http://domain.com/random/rand/random.php must show http://domain.com/index.php content
I tried below code , but still i get not found error
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/(.*?)/?$ /$1/index.php [L]

The problem here is that you are including part of the requested path in the rewritten URL, by using the $1, which effectively inserts the first bracketed part of the rule into the new URL. This means that your request for http://domain.com/random/rand/random.php would try to return the file http://domain.com/random/index.php.
In addition, your rule woudn't match your first example, as that URL doesn't contain a /, which is required by your regex.
Instead, if the requested URL isn't a file or directory (or link), then just rewrite everything to index.php:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php [L,R]
Note that I've added the R flag, which means that the requesting system (the browser) will see that the URL has changed... not sure if this is what you want.

Related

I want to rewrite https://blog/home/post?read=example to https://blog/home/example with .htaccess

I have tried many methods but none has worked.
Blog is the name of the domain, Home is a folder in the domain and post.php is the page getting details from database.
So, in my domain, I have:
home/post.php
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_-]+)$ /post?read=$1 [L] # Handle page requests
Above is the last code I used and it is not working. I'm getting a 404 error.
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_-]+)$ /post?read=$1 [L] # Handle page requests
The .htaccess is located in the root of the folder.
You seem to be ignoring the home subdirectory. (Although you've referred to both home and Home in your question - note that this is case-sensistive.) And if "/post is a PHP file" then you should presumably be rewriting to post.php, not simply post?
Note that Apache does not support line-end comments, as you are using here. (At best they get ignored, at worst they trigger a 500 error.)
If the .htaccess file is located in the document root, as you suggest then you would need to write the rule as follows:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^home/([\w-]+)$ home/post.php?read=$1 [L]
The \w shorthand character class is the same as [A-Za-z0-9_].
The RewriteRule pattern matches the URL-path relative to the location of the .htaccess file.
If, on the other hand, the .htaccess file is in the /home subdirectory (ie. at /home/.htaccess) then you would write the rule like this instead:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ post.php?read=$1 [L]
Note, there is no slash prefix on the substitution string.

MultiViews is *too* tolerant of bad URLs

When I enable MultiViews, if I visit bad URLs, the my page (index.php) is still reached, when I want the user to get a 404 error instead. I'm trying to figure out how to fix this without creating rules in my .htaccess.
For example, "www.mydomain.com/index/blah/blah", visits index.php, but I want it to fail due to the extraneous trailing garbage URL components. Similarly for "/contact/blah/awuihda/hiu", which shows the content of contact.php, should give a 404 error, because "/blah/awuihda/hiu" doesn't exist.
If I disable MultiViews it works fine, but then I can't abbreviate the URL as much as I want to (for example, can't type "/contact" to bring up "contact.php").
You could just use the following so the .php extension is not required, which is the usual approach:
RewriteEngine on
# Remove .php if it's present with a 301 redirect
RewriteRule ^(.+)\.php$ $1 [R=301,L]
# If a request doesn't exist as a directory, file or symlink
# and it does exist with .php appended, rewrite to that
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]
I know it's adding a rule to .htaccess but it's a one off that works for everything and also means you're not hitting potential duplicate content allowing the same thing to be served with or without .php (or indeed with anything at all trailing after it as in your examples). Hope it's useful.
It could go in main server config but would need altering.
I found a solution which works for me.
Options -Indexes SymLinksIfOwnerMatch -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Source: link

How to catch and redirect not-found pages in a sub-directory

I have an existing site with a bunch of pages in /widgets/ and the new site also has pages in /widgets/, some of which correspond to existing URLs. However, lots of other pages in that widgets section no longer exist and I just want to redirect them to /widgets.
Basically, I want to write a rule that catches pages that are not found and redirect them to the landing page (/widgets/) whilst allowing legit pages to be found.
Is it possible to write rules that will do this or do I need to manually redirect them all?
EDIT: As this is using a CMS (Craft) there are existing rules going on:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]`
UPDATE: Since these pages only exist in the CMS itself and not as actual files on the filesystem then the CMS is also going to have to manage the redirection. Apache (.htaccess) does not know what is or is not a valid page, so unless you can determine a (filename) pattern to these redirects, you would need to specify each file/redirect manually in .htaccess.
Since this is an external redirect, it should come before your existing rules:
RewriteEngine On
RewriteBase /
# Redirect non-existent files within /widgets/ to /widgets
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^widgets/. /widgets [R=301,L]
# Existing rewrites
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
Note that the RewriteRule checks for the /widgets/ directory, not RewriteCond directive - which would be less efficient.
I assume you already had a RewriteBase directive in your original code? This is required since you have a relative path substitution in your RewriteRule.
You can catch missing files in htaccess with -f
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f // if not a file
RewriteCond %{REQUEST_FILENAME} !-d // if not a directory
RewriteCond %{REQUEST_URI} /widgets/(.*) // if in directory /widgets
RewriteRule ^.*$ /widgets [L] // redirect to /widgets
You can leave out the third line if you want to redirect missing pages outside the widgets sub directory.

RewriteRule not working fine

I wrote the following rule in .htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/$ profile.php?business=$1
When i enter the URL like
http://www.abc.com/mujeeb/
page is correctly transfered to profiles page and page looks fine.
But i enter this in URL
http://www.abc.com/mujeeb
page doesn't show.
Can you please tell why? Or write the rule for this? i tried many times but not sucessful.
Mujeeb.
page doesn't show. because you specified that you RewriteRule is applied to the URL's ending with / at the end. Rewrite it as
RewriteRule ^(.*)/?$ profile.php?business=$1 [L]
And I hope that you have additional RewriteCond statements in order to prevent the infinite loop with redirects.
ps: basically you can prevent loop in two way
1) checks that requested url does not correspond to the existing file or directory. it is, probably, the best way to do (read comments to the second method)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ profile.php?business=$1 [L]
2) checks that you are requesting not the file from RewriteRule. This method is not good, because for each request, even for existing files and directories, it calls profile.php script
RewriteCond %{REQUEST_URI} !profile\.php$
RewriteRule ^(.*)/?$ profile.php?business=$1 [L]
It is because you check for the trailing slash with ^(.*)/$. If you add a question mark, the trailing slash will be optional.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)/?$ profile.php?business=$1
The RewriteCond is neccessary to make sure the Rule will only be applied once. Otherwise Apache will be caught in an infinite loop.
Try this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)[/]?$ profile.php?business=$1
That makes the last slash optional.
Well you rule is checking for a trailing slash in URI and that's the reason /mujeeb/ works but /mujeeb does not. Change your code to:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
# If the request is not for a valid file
#RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid directory
#RewriteCond %{REQUEST_FILENAME} !-f
# your rule without trailing slash
RewriteRule ^(.*)$ profile.php?business=$1 [L,QSA]
Plenty of good answers already. My answer is a bit different.
This is what I usually do. If the requested URL doesn't end with a /, I make the browser redirect to a URL with the trailing /. This is consistent with the default behaviour of Apache (due to mod_dir). So, this is how I solve this problem.
RewriteEngine On
# Canonicalize http://example.com/mujeeb to http://example.com/mujeeb/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)([^/])$ /$1$2/ [R=307,L]
# Let profile.php process http://example.com/mujeeb/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ profile.php?business=$1

htaccess to redirect ANY request

two parts....
PART 1 - I want to redirect any request to a directory that DOES NOT exist...
to a it's new location... AND and file that does not exist - to it's new location.
so: myDomain.com/myFolder (which no longer exists)
gets redirected to myDomain.com/newLocation/myFolder
and / or
myDomain.com/myFolder.htm (which does NOT exist)
gets redirected to myDomain.com/newlocation/myFolder
this is close...
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^myFolder http://myDomain.com/newLocation/myFolder
But I don't necessarily want to TYPE every questionable folder name, I'd prefer to have the 'newLocation" directory checked before the request fails...on any request, not just things i explicitly name
PART 2
Can someone explain the symbols used in the htaccess - or point me to a list...
example:
RewriteCond %{REQUEST_URI} ^/$ Rewriterule ^(.*)$ %{REQUEST_URI}/
I look at that and I know there are meanings t0 ^/$ etc - smacks of RegEx (not familiar)
or this
RewriteCond %{REQUEST_FILENAME} !-f (what's the !-f mean ????)
RewriteCond %{REQUEST_FILENAME} !-d (what's the !-d mean ????)
RewriteRule (.*) index.php?_var=$1 [L] ( the $1 means ...)
I want to understand the symbols and the syntax...
For Part 1 here is the rule will need:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# if requested URI is not a file and not a directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# externally redirect to /newLocation/{URI}
RewriteRule ^ /newLocation%{REQUEST_URI} [L,R=301]
For answer to part 2 here is the Apache mod_rewrite official documentation