Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite rule to return status 200 for certain URLs

I want URLs with a specific path to return an automatic 200 status response. I have tried the below, but get a an error when I try to start Apache:

First error: RewriteCond: bad flag delimiters

RewriteEngine On
RewriteCond %{THE_REQUEST} GET /the_url/
RewriteRule ^ - [R=200]

If I remove the path part then I do not get the error:

RewriteEngine On
RewriteCond %{THE_REQUEST} GET
RewriteRule ^ - [R=200]

But, of course, I need a way to include the path requirement.

Another error is that, even when the server does return a status 200 above (verified in Developer Tools), then the page still displays an error message: "OK The server encountered an internal error or misconfiguration and was unable to complete your request...." Is it not really returning a status 200? Or is it, but this is just what the default HTML page is when nothing is provided by the server?

like image 931
user984003 Avatar asked Jan 07 '18 18:01

user984003


People also ask

What is RewriteRule * F?

F|forbidden The following rule will forbid .exe files from being downloaded from your server. RewriteRule "\.exe" "-" [F] This example uses the "-" syntax for the rewrite target, which means that the requested URI is not modified. There's no reason to rewrite to another URI, if you're going to forbid the request.

What is $1 RewriteRule?

In your rewrite, the ^ signifies the start of the string, the (. *) says to match anything, and the $ signifies the end of the string. So, basically, it's saying grab everything from the start to the end of the string and assign that value to $1.

How do I rewrite a rule in Apache?

A rewrite rule can be invoked in httpd. conf or in . htaccess . The path generated by a rewrite rule can include a query string, or can lead to internal sub-processing, external request redirection, or internal proxy throughput.

What is RewriteRule in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.


1 Answers

The following did the trick. From https://httpd.apache.org/docs/2.4/custom-error.html

Enable the mod_rewrite module

LoadModule rewrite_module modules/mod_rewrite.so

and then add the following to your virtual host.

ErrorDocument 200 "ok"
RewriteEngine On
RewriteRule "/the_url/" - [R=200]

EDIT:

My original answer has been edited so much by someone else that it is no longer quite the same. I'll leave the edited answer (above), but I did not have to enable the mod_rewrite module. Some Linux distributions come with this by default or your host might have provided it as their default starting point. I added the three lines to httpd.conf.

like image 107
user984003 Avatar answered Oct 21 '22 02:10

user984003