htaccess / 404 issue for specific files - .htaccess

I have recently uploaded a new site and therefore had to redirect certain pages.
There are some pages from the old site which are fine to 404, so the client can do whatever they need to do at their end.
The problem I have is that these do not throw a 404.
Example:
On the new site I have /servers and then furthermore: /servers/hp
These are in the htaccess file as: RewriteRule ^servers servers.php [NC] and RewriteRule ^servers/([^/\.]+)/?$ server.php?s=$1 [L]
However, on the old site there was a file /servers/hp-alphaserver.php - this does not exist anymore - but there is no 404, instead it shows the page for /servers
Hope that makes sense.

Found it:
RewriteCond %{DOCUMENT_ROOT}servers/([^/\.]+)/$0 !-f
RewriteRule ^servers/([^/\.]+)/[^/]+$ 404.php [L]
This checks to see if the file actually exists.

Related

Redirect all subdomains and subdirectories to index page using .htaccess

I have a Detroit iOS & Android Mobile App Development website that only has one web page : index.html.
The source code of the site is here.
Instead of showing a 404 error page, I want to redirect the user to thefirstprototype.com if they try to go anywhere else or try to put anything after.
For eg:
mail.thefirstprototype.com takes the user to just thefirstprototype.com
thefirstprototype.com/mail takes the user to just thefirstprototype.com
I know it's possible to do it using a .htaccess in the root folder, but I am just not sure how. There are a lot of tutorials showing how to do it between different domains, but nothing to my specific case. How do I do it?
Thanks
Edit1: Please note that I am not using any CMS like Wordpress. I am just plain FTP to push a static HTML, CSS, JS webpage to the hosting server
Try the following:
DirectoryIndex index.html
RewriteEngine On
# Redirect non-canonical hostnames (eg. mail)
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^ http://example.com/ [R=302,L]
# Redirect 404 to root
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [R=302,L]
However, whether this catches requests for the mail. subdomain will depend on whether that subdomain points to the same place as your main domain. (For cPanel shared hosting, that is not necessarily the case.)
Change the 302 (temporary) redirect to 301 only once you have tested that this works OK - to avoid potential caching issues associated with 301 (permanent) redirects.
As an added bonus, you could redirect any direct requests for index.html back to the root. For example, add the following between the above two rule blocks:
# Remove "index.html" if requested directly
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.html$ / [R=302,L]
The condition that checks against the REDIRECT_STATUS environment variable is to ensure we don't get a redirect-loop since mod_dir internally rewrites the request to index.html.

.htaccess Redirect To Another Site's Homepage Only If URL Don't Exist

So this is what I'm trying to accomplish, and I'll do my best to explain...
Through the .htaccess file, I'm attempting to redirect all pages of one site to another site, but ONLY if the URL doesn't exist. If the URL exists, then do nothing. Load that URL. If the URL doesn't exist, redirect to the homepage of another site.
So, going to www.johnny.com/boxing (no files) or even just www.johnny.com (no files) would redirect to www.johnpunches.com, since neither of those URLs work. And that would work for any URL on www.johnny.com that doesn't exist. It would simply redirect to the index page of www.johnpunches.com
However, going to www.johnny.com/members (where there is a working directory) would actually go to that URL and not redirect, because the page exists.
So, again, if the URL exists, then do nothing. Load that URL. If the URL doesn't exist, redirect to the homepage of another site.
RewriteEngine On
# File does not exist
RewriteCond %{REQUEST_FILENAME} !-f
# Directory does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# Redirect to new domain if the above were missing
RewriteRule ^(.+)$ https://www.google.com/$1 [NC,QSA]
Change out google.com to whatever domain you are redirecting to.
Okay, I was able to get some assistance from another thread and put this together:
RewriteEngine on
RewriteCond %{http_host} ^johnny.com [nc]
RewriteRule ^(.*)$ http://www.johnny.com/$1 [r=301,nc,l]
RewriteRule !^folder($|/) http://www.johnkicks.com [L,R=301]
In the example above, the "folder" can be replaced with any directory that works. The second and third line force the www version of the site before the final rule has run so that any URL that DOES work, like johnny.com/folder would redirect to www.johnny.com/folder.
Two questions:
Did I do it correctly? Does the order of my rules work? Should I be doing it any differently?
Would I have to duplicate that fourth line for every working directory?

not found error in webmaster when redirection handled through .htaccess

I am trying to find answer for this for a long time.
I have created one site and i handle the not found page(404 pages) through .htaccess.
Everything works great but when i submitted to google webmaster for indexing (in fetch as google) it showing an error ( status = not found).
Does anyone know How to tell google to handle these pages (index these pages)
my .htaccess file is
ERRORDOCUMENT 404 /redirect.php
in my redirect.php file i take the necessary action.
like if URL is www.mysite.com/username then profile of that user is shown
Thank in advance
Google will never index 404 error pages.
You can use .htaccess with:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /redirect.php [L]
which redirected all links to pages that do not exist
if you use:
RewriteRule ^ /redirect.php [R=301,L]
the link change in the browser. But with [L] you can show your page without link change.

How to delete a page to show 404 server error through htaccess

I am unable to remove a page of a component of Joomla site, due to dynamic nature of it as created.
How is it possible that i can display 404 server error, if that exact url is opened or linked by by any one
The page is
www.abc.com/index.php?option=com_forum&view=ask
May be through htaccess, if there is any possibility.
Insert this code in your main .htaccess:
RewriteCond %{QUERY_STRING} ^option=com_forum&view=ask$ [NC]
RewriteRule ^index\.php$ - [NC,L,R=404]

404 redirect using .htaccess

I want to set up a rule in my .htaccess file so that any url that is enetered, that results in a 404 because there is no such file, automatically re-directs to the home page of the site:
index.php
my .htaccess file looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^queenslandbeerweek.com.au$ [NC]
RewriteRule ^(.*)$ http://queenslandbeerweek.com.au/$1 [L,R=301]
ErrorDocument 404 /index.php
This causes the index.php file to show but is broken and leaves the eroneous URL in the address bar.
I have read in the answer to another post that it has something to do with passing the erroneous URL as a parameter, causing the page to not load properly, because the page calls data from a database and it is passing the bad URL as a parameter of index.php but there was no hint as to what the solution is.
What I would like to happen, is if an incorrect URL is typed into the address bar, or if a link is followed, to a file that does not exist, the completely forget about this file, drop everything, and go to the home page index.php.
index.php calls data from a database
Is this possible using a .htaccess file?
I have exactly the same problem with another of my sites.
Any help would be greatly appreciated.
Cheers, Al.
I dont think you can directly redirect an error document but you can catch nonexistent files and folders
!-f means not a file !-d means not a directory, $1 is whatever is in (.*) (the path in the url)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?errorurl=$1 [R=301,L]
ErrorDocument 404 /404.php
You can place the 404 error template anywhere you want. For example you could place all error messages in a folder called errormessages
ErrorDocument 404 /errormessages/404.php

Resources