How to set up an internal redirect in Apache Webserver


TL;DR for how to set up an internal redirect in apache:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^index\.html new.html [PT]
</IfModule>
Specification:

OS Version: Ubuntu Os 18.04.3 (LTS) x64
Apache version: Apache/2.4.29 (Ubuntu)

This example code will issue a redirect internally if /index.html is requested from it to /new.html. Internal redirect is issued if the flag [PT] is used, otherwise, without the flag, it would be regular 302 (Temporary redirect).

Internal rewrite diagram
Internal rewrite diagram

Test internal redirect in Apache

Before we use the redirect configuration let’s see what is the content length for index.html so we can compare the headers after the code is used:

bluegrid-edu:~# curl -I http://bluegrid.io/index.html
HTTP/1.1 200 OK
Date: Sun, 26 Jul 2020 19:44:09 GMT
Server: Apache/2.4.29 (Ubuntu)
Last-Modified: Sun, 26 Jul 2020 15:12:25 GMT
ETag: "2aa6-5ab59a23dd33d"
Accept-Ranges: bytes
Content-Length: 10918
Vary: Accept-Encoding
Test: 1
Content-Type: text/html

Now. when we add the code we can check the response and see if the content length is changed:

bluegrid-edu:~# curl -I http://bluegrid.io/index.html
HTTP/1.1 200 OK
Date: Sun, 26 Jul 2020 20:06:53 GMT
Server: Apache/2.4.29 (Ubuntu)
Last-Modified: Sun, 26 Jul 2020 20:06:33 GMT
ETag: "10fe-5ab5dbe1ff88b"
Accept-Ranges: bytes
Content-Length: 4350
Vary: Accept-Encoding
Test: 1
Content-Type: text/html
Share this post

Share this link via

Or copy link