Saturday, October 13, 2018

Apache Httpd 2.4 Rewrite Rule to return 301 redirect, but returns 302



I have two different examples of where Apache Httpd 2.4 on Centos7 is configured to perform redirects with the R=301 flag, but they are actually returning 302 redirects. One is an extremely simple case, just redirecting http to https. Here is the extent of the rewrite rules:




... other content ...
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !https

RewriteRule ^/(.*)$ https://%{HTTP_HOST}/$1 [NC,R=301,L]



Why is this returning a 302 redirect instead of 301?



# apachectl -S
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80 example.com (/etc/httpd/sites-

enabled/example.com.conf:5)
*:443 example.com (/etc/httpd/sites-
enabled/example.com.conf:23)
Syntax OK

Answer



Your problem is that you are not using the appropriate hostname for curl. The rewrite rule is triggered on condition of matching hostname example.com. If you are just using curl http://localhost then httpd gets the hostname "localhost" and the rule does not match. Instead use the curl command with a --proxy argument pointing at localhost (be sure to specify the port number!) and the url argument matching the expected host name. Like this:



curl --proxy localhost:80 http://example.com


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...