Apache htaccess return 404 code even if page exists without using rewrite mod: - apache

To protect my server from bots, I want to return a 404 error page if certain files are requested EVEN IF THEY EXIST but without using the rewrite mod. It is possible?

You can use a mod_alias Redirect (or RedirectMatch) directive.
For example:
Redirect 404 /file1.html
Redirect 404 /file2.html
This doesn't actually trigger a "redirect", as in an external 3xx redirect. It sends the stated 404 response if one of the URLs is requested (regardless of whether that file exists or not).
Reference:
https://httpd.apache.org/docs/current/mod/mod_alias.html#redirect
If there is a pattern to these URLs then use a RedirectMatch directive instead, which matches against a regex rather than using simple prefix-matching. For example, to serve a 404 for all requests to the /secret subdirectory (and all files within) then use the following:
RedirectMatch 404 ^/secret($|/)
To customise the 404 response use an ErrorDocument directive:
ErrorDocument 404 /error-docs/404.html

Related

How to 404 urls in .htaccess?

I'm trying to make all urls for https://www.example.com/page/ redirect to 404.
The problem I'm having is every number after /page/ is redirecting to the homepage, and is getting indexed - causing a duplicate content penalty.
So far I have around 30 pages indexed, which all redirect to the homepage.
I'd like to have any URL which has a number after /page/ to 404 (with the 404 header), so I can deindex all of these pages.
So far I've tried:
Redirect 404 ^page/(.*)$
or
Redirect 404 /page/*
Unsurprisingly these haven't worked - where am I going wrong?
Redirect 404 ^page/(.*)$ or Redirect 404 /page/*
You are close, except that the Redirect directive uses simple prefix-matching, it does not use a regular expression (regex) or wildcard match.
to make all urls for https://www.example.com/page/ redirect to 404.
To make all requests for /page/ result in a 404, regardless of whether a number follows it or not, you would use the following:
Redirect 404 /page/
As noted above, the Redirect directive uses simple prefix-matching, so the above serves a 404 for any URL that starts with /page/.
However ....
any URL which has a number after /page/ to 404
To specifically serve a 404 for URLs of the form /page/<number> only and not /page/ then you would need to use a RedirectMatch directive instead that uses regex to match and not prefix-matching.
For example:
RedirectMatch 404 ^/page/\d+$
\d+$ matches 1 or more digits to the end of the URL-path. So the above will match /page/1 and /page/123456 but not /page/ or /page/abc or /page/123z etc.
If instead you wanted to match /page/<something> where <something> is literally anything, but not match /page/ only. Then you could instead use the following:
RedirectMatch 404 ^/page/.
The above matches /page/ at the start of the URL-path followed by at least 1 other character.
Note that whilst we are using the Redirect and RedirectMatch directives here, there is no external redirect (3xx response). The 404 is served by Apache as an internal subrequest. Apache sends the 404 Not Found header.
However, if you specifically want to "deindex" these pages quicker then consider sending a "410 Gone" instead. This is a stronger signal for search engines that the page is not coming back. In this case you can also use the gone keyword in place of the 410 status code.

Rewrite language code into url if missing

I want to change an existing Magento store to add store/lang codes to the url i.e.
http://mystore/en/PRODUCTXYZ.html
http://mystore/de/PRODUCTXYZ.html
Old links to http://mystore/PRODUCTXYZ.html will now throw a 404 error.
How can I create an Apache url rewrite rule to add a language code if it is missing i.e. rewrite
http://mystore/PRODUCTXYZ.html
to
http://mystore/de/PRODUCTXYZ.html
So that old links 301 redirect to the correct product.
I have worked around this with
Redirect 301 /PRODUCTXYZ http://mystore/de/PRODUCTXYZ.html
But obviously for thousands of products this might not be practical.
You can redirect multiple Product.html urls with just 1 line of code using RedirectMatch :
RedirectMatch 302 ^/([^/.]+)\.html$ http://example.com/de/$1.html
I used 302 for testing purposes and to avoid browser's cache,
change 302 to 301 (permanent redirect) when you are sure the redirect is working.

htaccess - 404 redirect from different folder into root

I looking for way to redirect any 404 error into index.php
ErrorDocument 404 /index.php
Works fine until you have a 404 inside another folder /about/non-existing-page.php - in this case it will display index.php but the page will be missing styles (as styles are in the root folder).
Any way I can actually user 301 redirect for ANY (anywhere) 404 error?
As you already said, this is not the redirection page's fault: it's your browser that thinks it's in the /about folder. There's nothing you can do in .htaccess to change that: a 301 redirect would change the status code, which is not a good idea.
The easiest solution would be using absolute links in the document:
<img src="/images/image.jpg">
for completeness' sake, a subsequent 301 can be achieved by using a full URL:
ErrorDocument 404 http://mydomain/index.php
but as said, it's probably not a good idea.
You don't need to use absolute links in your 404 document.
If the file is placed in the root, add
<base href="/">
to the head section of your 404 document and it will be shown correctly even if invoked by a 404 inside a subfolder.

Redirect via htacces to error pages (not found & Internal Server error)

can I vía .htaccess redirect when
Users finds an Internal Server Error
A not found page
Is that posible? if so, can someone help me with the rewrite rules?
edit
trying
ErrorDocument 500 /oohps.php
ErrorDocument 404 /where.php
and adding them at domain.com/oops.php and domain.com/where.php but still not loaded
Why not just use custom error responses via ErrorDocument?
ErrorDocument 500 /errors/internal-server-error.html
ErrorDocument 404 /errors/not-found.html
It is useful I am able to redirect to index page on 404 error(page not found) by modifying "ErrorDocument" in /etc/apache2/conf.d/localized-error-pages file.
Thanks.....

Redirect 404 to another domain with apache?

Hey guys, have a question regarding apache. I have a site that's been re-engineered, but I want to capture all the 'old' links that people may have bookmarked or come from search engines to the old site which is under a new domain name. How do I get apache to redirect only 404 not found to the old site?
TIA,
J
You should first decide what status code you want to send. Sending both a 404 status code and a redirect is not possible.
But seth did already mention the right method, the ErrorDocument directive:
# local path
ErrorDocument 404 /local/path/to/error/document
# external URI
ErrorDocument 404 http://uri.example/to/error/document
If you use a local path, the 404 status code is sent. If you use an absolute URI, a 302 status code (temporary redirect) is sent.
And if you want to send a 301 redirect:
Redirect 301 / http://new.example.com/
Your old domain should capture all responses and return a '301 moved permanently' response with the new domain in the 'Location' field of the header. A 404 means 'not found' and in this case it's not strictly true.
Another option, similar to that proposed by #seth is to add the handler so it points to a static html page which you can use to explain to the user what happen, and present them with options.
You can include a meta redirect so that if they don't do anything after a few seconds they're automatically redirected.
Which option will work best is really up to you do decide.
You could set your 404 document to a CGI that redirects the user.
ErrorDocument 404 /cgi-bin/redirect-to-other.cgi