2

I have an Apache server hosted on an EC2 instance which points to a domain that I own. Now, what I essentially want to get done is that the web server should return a .json file when the user tries to curl the domain.

eg. $curl mydomain.com

My 000-default.conf file which is located in /etc/apache2/sites-available/ looks as follows. I've added the <Directory> section to activate the .htaccess file located in my /var/www/html folder which is also the root of my website.

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<Directory /var/www/html/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

My .htaccess file looks as follows:

RewriteEngine On
RewriteCo "%{HTTP_USER_AGENT}" curl
RewriteRule ^(.*)$ "/var/www/html/resume.json" [L,R=302]

My /var/www/html directory looks something like this:

/.well-known
.htaccess
index.html
resume.json

I've gone through numerous examples and tutorials and have used bits and pieces from everywhere to reach so far as I need something specific to be done. Unfortunately, when I try to curl my domain I get returned by the following message.

C:\Users\example >curl example.com/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="https://example.com">here</a>.</p>
<hr>
<address>Apache/2.4.29 (Ubuntu) Server at example.com Port 80</address>
</body></html>

I would like to know what I'm doing wrong in terms of configuration that I cannot get the .json displayed as it is on the terminal.

MrWhite
  • 11,643
  • 4
  • 25
  • 40
jait
  • 23
  • 2

1 Answers1

2

When you are using CURL, you need to use the -L flag to follow redirects, otherwise you will just see the redirect response, which is what you are seeing. (Since that is what your .htaccess directive is doing.)

However, there are errors in your .htaccess file...

Your .htaccess file (from your "screen grab"):

RewriteEngine On
RewriteCo "%{HTTP_USER_AGENT}" curl
RewriteRule ^(.*)$ "/var/www/html/resume.json" [L,R=302]

That RewriteCo line is an obvious syntax error - so I assume that can't be in your actual file?

  • "/var/www/html/resume.json" - In .htaccess, the RewriteRule substitution takes a URL-path (relative to the document root) - you have supplied an absolute filesystem path, which won't work.

  • ^(.*)$ - Your RewriteRule pattern also matches everything, so this will result in a redirect loop. If you only want to redirect the document root then you should use ^$ instead.

Try the following instead:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} curl
RewriteRule ^$ /resume.json [R=302,L]

However, do you really want to externally redirect the request? This should probably be an internal rewrite:

RewriteRule ^$ /resume.json [T=application/json,L]

Aside: It looks like your <Directory> section is "floating" outside of your <VirtualHost> container - shouldn't this be inside the vhost?

MrWhite
  • 11,643
  • 4
  • 25
  • 40
  • spot on! Thank you for your time. I've got it working now but curl requires "-L" flag without which it still says "302 Found". Is there a way to work around it so that I only have to type "curl example.com" without the "-L" flag and still get the desired output? Also, yes the section has to go outside my , I just checked and it didn't work when i tried otherwise. – jait Jan 25 '19 at 01:47
  • As mentioned above, you can change this to an _internal rewrite_ instead of an _external redirect_. But if you keep it as a "redirect" then you will always need to include the `-L` flag with cURL. – MrWhite Jan 25 '19 at 01:54
  • well it is set to the latter RewriteRule that you've mentioned but still requires "-L" flag. I'm unsure why is this the case. – jait Jan 25 '19 at 02:03
  • The later, as in `RewriteRule ^$ /resume.json [T=application/json,L]`? That is entirely internal to your server, there is no "redirect". This should not require the `-L` flag. Make sure you are not seeing a cached response from your server. – MrWhite Jan 25 '19 at 02:11
  • yes it is `RewriteRule ^$ /resume.json [T=application/json,L]` but still requires -L – jait Jan 25 '19 at 02:13
  • If you don't use the `-L` flag what response do you get? – MrWhite Jan 25 '19 at 02:14
  • `C:\Users\user.name>curl ------b.me 302 Found

    Found

    The document has moved here.


    Apache/2.4.29 (Ubuntu) Server at -----b.me Port 80
    `
    – jait Jan 25 '19 at 02:15
  • I've also restarted the server after making changes to the file. just to be sure. – jait Jan 25 '19 at 02:16
  • The redirect you are seeing seems to be an HTTP to HTTPS redirect, unrelated to the above. You need to include the protocol in the request, ie. `curl https://example.com`. – MrWhite Jan 25 '19 at 02:22
  • yes, got it! Thanks a ton!. Are there any beginner friendly tutorials that you could point me to? I want to learn this stuff(configurations etc). Seems pretty interesting. DOCs seem overwhelming at the moment. – jait Jan 25 '19 at 02:25
  • Glad you got it sorted. I'm not sure off hand, but you've touched on quite a few different topics here... HTTP, cURL, Apache, mod_rewrite. – MrWhite Jan 25 '19 at 02:32
  • yes! well i could have done this on Caddy Server with just about 2 lines of configuration but that seemed too easy. Apache was quite a bit of task to play with. Thanks to you! have been playing with it for a few days now without any better results. – jait Jan 25 '19 at 02:55