How to use .htaccess to redirect missing files? - apache

I've been hosting some files which aren't on the server anymore and apache error log keeps bugging me with the "file does not exist" line.
I'd like to use .htaccess to manually specify which files, if requested in the URI, should redirect to the root location (or any else, thereof).
Could anyone help me how to do that?
Example:
www.domain.com/non-existent-file.jpg should go to www.domain.com/

Solved it with:
RewriteRule non-existent-file.jpg http://www.domain.com/ [R=301]
Hope that's the best solution!

Related

I can't redirect /domain.com/.html to index.php

Search Console Error is showing me this URL:
/domain.com/.html as an error.
If I put that url in my browser I get:
Forbidden
You don't have permission to access /.html on this server.
this situation is because Apache is searching a file named .html how can I add a rule for htaccess file to fix this situation? if the URL is /.html redirect to index /
Good Morning,
why exactly do you want /.html to be a valid call? Shouldn't it be /specificName.html like /stack.html?
Anyway, try something like this:
RewriteRule /(specificName)\.html$ / [R=permanent,L]
If you really meant /.html just leave the bracket out.
Hope it helps :)

.htaccess Rewrite directory/file to new directory/file

I'm trying to rewrite a directory and filename at the same time using .htaccess, but for some reason I can't find anything about such a case on the net. Tried googling what not, but there is simply not an example for this.
When I access a php file in the following directory:
website.com/somedir/somefile.php
I want to open a file that is contained at:
website.com/dirone/dirtwo/dirthree/dirfour/anotherfile.php
Tried simply 301 redirecting the directory
website.com/somedir/
to
website.com/dirone/dirtwo/dirthree/dirfour/
with
Redirect 301 http://website.com/somedir/ http://website.com/dirone/dirtwo/dirthree/dirfour/
But that would only work if the file name was the same. However in my case the filename changes from somefile.php to anotherfile.php as well.
Also tried something like:
RewriteRule ^somedir/somefile\.php website.com/dirone/dirtwo/dirthree/dirfour/anotherfile.php [L,QSA,NC]
But it did not work.
Any help would gladly be appreciated.

How do I rewrite www.sitename.com/thing/thing.php?otherthing=something-like-this to www.sitename.com/something-like-this?

How do I rewrite
www.sitename.com/thing/thing.php?otherthing=something-like-this
to
www.sitename.com/something-like-this?
please help me with this as I can't seem to succeed. My host uses apache 2.2. Many thanks for your help!
Update
No I don't need that trailing ? However, I used the Rewrite rule you offered me and it still ain't working. I also added a RewriteEngine On before the rules.
I have Linux hosting, .htaccess and the code is obviously semantically correct, cause otherwise I would get the all so popular 500 internal server error. I placed the .htaccess file in the folder thing and in the root of the site, but it still won't work.
There should be an option to display it in directory format instead of the PHP ? format. If not, you could use the .htaccess mod_rewrite rule to make that display in the /folder/ way.
The way I do it is that I just upload my files and each page name is index.html and then I create folders, and put each index.html in the folder. Like this:
/guidelines/
In that folder is index.html, so instead of it being /guidelines.html it's /guidelines/
Looks better without .html
You need to use mod_rewrite:
RewriteCond %{QUERY_STRING} ^otherthing=(.*)$
RewriteRule ^thing/thing.php$ /%1? [L]
No idea if you meant to have that trailing ? at the end of the rewrite, I don't think that's possible. Note that the ? at the end of the RewriteRule is to get rid of the query string, otherwise, the rewritten URL will still have the ?otherthing=something-like-this at the end.

.htaccess mod_rewrite rule not working in Ubuntu

My apologies if this is an easy one. I have Googled it up the hizzy to no avail.
I am running Ubuntu 9.04, Jaunty Jackelope and Apache2. After much trouble, I finally enabled mod_rewrite, and my .htaccess file is attempting to do it's thing, but is failing. This is my setup.
In /etc/apache2/conf.d/ I have a file called apeace-public-html.conf. It reads as follows:
# makes /home/apeace/public_html/ available through /localhost/apeace
Alias /apeace /home/apeace/public_html/
And in /home/apeace/public_html/ I have the following .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^test\.html$ index.html
Also in /home/apeace/public_html/ I have a file named index.html, but I do NOT have a file named test.html. It seems to me that this rewrite should show index.html when I try to access http://localhost/apeace/test.html. However, this is the error I get:
Not Found
The requested URL /home/apeace/public_html/index.html was not found on this server.
So the question is, what in the world am I doing wrong?
Much thanks.
-apeace
Just a guess here, but can you try to make the RewriteRule like ^test.html$ /apeace/index.html
From the error message, it seems it is translating `http://localhost/apeace/test.html to http://localhost/home/apeace/public_html/index.html
Your rewrite rule is working correctly since it's telling you it can't find "index.html". If you went to test.html and it said it can't find "test.html" then your rewrite rule would be at fault.
So what this means is that something else is wrong in your setup, whether it's a bad file or directory name somewhere, or whatever else. Make sure there's nothing basic you're overlooking.
But in answer to your question (especially the title), your htaccess is fine.

return 404 error on existing directory

Ok so i have a directory in my root folder called /pages in which i keep all my pages that i include thru index.php
What I wonder is would could i somehow return a 404 error if someone requests it? i dont like snoopy people...
Is there any way you can re-structure your app so that those pages are outside the document root? Much better answer...
Absolutely put the data outside the web root as gahooa says.
If that's totally impossible due to provider restrictions, then put a .htaccess with the following contents into the directory:
deny from all
that should block all requests to files in that directory and return a "401 forbidden".
Try this rule:
RewriteRule ^pages($|/) - [L,R=404]
But the R=404 flag does only work since Apache 2.
gahooa gives the best solution. If you can't place files outside of document root and your host runs Apache 1.3, you can also use mod_alias:
Redirect 404 /pages