Redirect 404 to another domain with apache? - 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

Related

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

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

Apache: forcing a 404 status code

I am working with a tomcat application that I cannot modify, though I have full control over apache which is set up in front of it (with a reverse proxy).
The application will do a 302 redirect to pagenotavailable.jsp when it encounters a URL that's no longer valid (i.e. the ID no longer exists or is malformed).
I need to figure out a way to make these return a 404 so that these URLs drop out of the search engine indexes.
One possibility I came up with is to set up a mod_rewrite redirect from pagenotavailable.jsp to a page I made called 404.html :
RewriteEngine on
RewriteRule ^/pagenotavailable\.jsp /404.html [NC,R=404,L]
Or just this, since 404.html is already set up as ErrorDocument:
RewriteRule ^/pagenotavailable\.jsp$ - [R=404]
That is showing the content of my custom 404.html page, but the URL does not update (it is still pagenotavailable.jsp) and the status code is still 302.
Any ideas why I don't get a 404, of any alternative approaches are appreciated!
If you are responding with a 302, then the client will always update its URL (e.g. in the URL bar) to show .../pagenotavailable.html. If you want that to go away, you'll have to redirect again to your preferred URL. The only other option would be to modify the application so it doesn't perform a redirect, but instead a "forward" which is handled entirely on the server-side. But you specifically mentioned that you can't modify the web app, soo...
I'd be very surprised if the status code were still 302... when a client receives a 302 response, it should perform a GET to the Location provided in the 302 response header. If anything, I'd expect a 200 response with your 404.html content if the response code wasn't being set to 404.
The [R=xxx] flag tells mod_rewrite to issue a redirect, which must be a 3xx response code. You can use a 404, but you should be aware of the caveats. That page says what happens with the Location header (i.e. nothing) and it says that it will imply the [L] flag, but it doesn't say anything about what response code will actually be sent to the client.
What about using RewriteRule to rewrite the page to something that actually doesn't exist?
RewriteRule ^pagenotavailabe.jsp$ /does-not-exist.html
... then let your standard 404 handler handle the error (and return the contents of 404.html plus the 404 protocol response).

How to keep URI in the browser using ErrorDocument in .htaccess?

I have a simple 404 error redirect in the .htaccess to my script like this:
ErrorDocument 404 https://example.com/error.pl?q=404
But on the redirect the address is changed in the address bar of the browser to this https://example.com/error.pl?q=404 from the wrong address (from which the redirect is performed).
How to keep the initial address in the browser address bar while redirecting to the custom error page?
Your kind help is highly appreciated!
Use link without domain name:
ErrorDocument 404 /error.pl?q=404
Note that when you specify an ErrorDocument that points to a remote
URL (ie. anything with a method such as http in front of it), Apache
HTTP Server will send a redirect to the client to tell it where to
find the document, even if the document ends up being on the same
server. This has several implications, the most important being that
the client will not receive the original error status code, but
instead will receive a redirect status code. This in turn can confuse
web robots and other clients which try to determine if a URL is valid
using the status code.
https://httpd.apache.org/docs/2.4/en/mod/core.html#errordocument

Capturing and logging broken links on a custom 404 page

I am trying to create a customer 404 page in php that captures and can log into a database the page that was requested. I have this in my .htaccess file already:
ErrorDocument 404 http://mywebsite.com/notfound.php
What I am trying to do is append the requested URI to as a query string to the URL. I have tried this:
ErrorDocument 404 http://mywebsite.com/notfound.php?url=%{REQUEST_URI}
but the URL shows the literal text that I put in above. So I figured I would need some sort of flag at the end like [QSA] or something but all attempts result in a server error 500.
My ultimate goal is to capture the attempted broken link and the referring URL (which I can get from $_SERVER['HTTP_REFERER']) on the notfound.php page and log this into my database where I can pull a report of all links, hyperlinked or called via AJAX, that are broken or missing. I am able to do everything except get the requested URI of the actual link that caused the 404 error.
You shouldn't use ErrorDocument 404 with http:// in the target to avoid full redirect, like this:
ErrorDocument 404 /devsite/notfound.php
Then original 404 URI will be available to you as $_SERVER["REQUEST_URI"]

forward Request parameters to custom 404 page using ErrorDocument in apache?

Is there a way to have the request parameters "forward on" to the custom error file in apache much like [QSA] does on rewrite rules?
if i send in www.foo.com/doesnotexist?bar=true
and i have this in vhost:
ErrorDocument 404 /customerrorpage
then it would call customerrorpage with:
customerrorpage?bar=true
I know this is really old question, but it hasn't been answered and so in case someone else is searching the site for the answer I'll share what I know.
The Apache 2.2 ErrorDocument page says that when you have a Custom Error page which is a local page then a number of environment vars will be set which can be read from your customer error handler. One of the vars is REDIRECT_QUERY_STRING which will contain the query string that the original page had. So in your example the REDIRECT_QUERY_STRING would contain 'bar=true'.