[24th grade] Beginner innocently tried setting redirect [Apache] [AWS]

Hello. I'm Inu, a humanities graduate and trainee infrastructure engineer who graduated in 2024.
These days, I want to be more alert than dizzy.

This time, I'd like to share what I, a complete beginner, have learned about redirect settings in a primitive, physical, and fetishistic way

What is a redirect?

First, let's take a look at Wikipedia's explanation

A redirect (forwarding) is a function that redirects a link to an article to another page. Such a page is called a redirect page (forwarding page)

Quote: https://ja.wikipedia.org/wiki/Wikipedia:%E3%83%AA%E3%83%80%E3%82%A4%E3%83%AC% E3%82%AF%E3%83%88#:~:text=%E3%83%AA%E3%83%80%E3%82%A4%E3%83%AC%E3%82%AF%E3%83% 88%EF%BC%88%E8%BB%A2%E9%80%81%EF%BC%89%E3%81%A8%E3%81%AF%E3%80%81,%E3%81%99%E3 %82%8B%E6%A9%9F%E8%83%BD%E3%81%AE%E3%81%93%E3%81%A8%E3%81%A7%E3%81%99%E3%80%82 (As of 2024/10/07)

That's exactly it

Have you ever been automatically redirected to a new page when you accessed an old homepage?

That is exactly what a "redirect" is.

Let's try setting up a redirect

How do you set up the redirect?

So, this time I would like to try redirecting from "hogehoge.info" to "www.hogehoge.info" by setting it up on the server side and by using AWS ALB

Verification environment

how to set it up on the server side , we used the following verification environment to set it up.

Virtualbox: Version 7.0.20

Vagrant: Version 2.4.1

Here's how to set up a testing environment with Vagrant:

Build your own testing environment with Vagrant

Vagrant box: AlmaLinux 9

Apache: Version 2.4.57

Step 1: Set it up on the server side

If you want to set it up on the server side, try adding an alias to the virtual host's conf file + adding a line to the virtual host's ".htaccess"

① Add an alias to the virtual host conf file

First, check the list of virtual hosts using the command below

httpd -S

The execution results show that this server contains "www.hogehoge.info" and "nova-prod.byd-edu.info"

■Execution result VirtualHost configuration: *:80 is a NameVirtualHost default server www.hogehoge.info (/etc/httpd/conf.d/hogehoge.conf:1) port 80 namevhost www.hogehoge.info (/etc/httpd/conf.d/hogehoge.conf:1) port 80 namevhost nova-prod.byd-edu.info (/etc/httpd/conf.d/nova-prod.byd-edu.info.conf:1) ServerRoot: "/etc/httpd" ~Omitted~

Now, let's use the less command to look at the conf file for "www.hogehoge.info"

less /etc/httpd/conf.d/hogehoge.conf

The settings are left as default, so we will add a "ServerAlias" here

■Execution results<VirtualHost *:80> ServerName www.hogehoge.info DocumentRoot "/var/www/vhosts/hogehoge.info/public_html" DirectoryIndex index.php index.html index.xml<Directory /var/www/vhosts/hogehoge.info/public_html > Options FollowSymLinks AllowOverride all</Directory> CustomLog "/var/log/httpd/hogehoge.info-access_log" combined ErrorLog "/var/log/httpd/hogehoge.info-error_log"</VirtualHost>

Use the vi command to edit the conf file for "www.hogehoge.info"

vi /etc/httpd/conf.d/hogehoge.conf

Enter edit mode and then add "ServerAlias" under ServerName

<VirtualHost *:80>ServerName www.hogehoge.info ServerAlias ​​hogehoge.info ←Here!! DocumentRoot "/var/www/vhosts/hogehoge.info/public_html" DirectoryIndex index.php index.html index.xml<Directory /var/www/vhosts/hogehoge.info/public_html > Options FollowSymLinks AllowOverride all</Directory> CustomLog "/var/log/httpd/hogehoge.info-access_log" combined ErrorLog "/var/log/httpd/hogehoge.info-error_log"</VirtualHost>

Once you have added the description, exit edit mode with esc and save and exit with :wq.
Use the cat command to confirm that the "ServerAlias" has been added correctly, then move on to the next step.

② Write in the virtual host's ".htaccess"

Next, let's create a ".htaccess" file.
First, move it to the document root of "www.hogehoge.info".

cd /var/www/vhosts/hogehoge.info/public_html

Once you have moved, create a ".htaccess" file in public_html

vi .htaccess

When you open the vi screen, a blank editing screen will appear, so add the following code

RewriteEngine on RewriteCond %{HTTP_HOST} ^hogehoge\.info [NC] RewriteRule ^(.*)$ http://www.hogehoge.info/$1 [L,R=301]

Let's take a look at what each of these means

RewriteEngine on Enable URL rewriting
RewriteCond %{HTTP_HOST} ^hogehoge\.info [NC] Define the condition so that the variable representing the URL host name = hogehoge.info, regardless of case
RewriteRule ^(.*)$ http://www.hogehoge.info/$1 [L,R=301] Define a rule that rewrites requests that meet the above conditions to www.hogehoge.info and redirects them with a status code of 301

Hmm, that's difficult.
I found this article very easy to understand.

URL normalization (redirection with mod_rewrite)

Once you're done editing, reload the configuration with the following command:

systemctl reload httpd

Finally, try actually accessing "hogehoge.info" and see if it redirects to "www.hogehoge.info"

Open Notepad with administrator privileges, and enter the local IP and hostname at the bottom of the "C:\windows\system32\drivers\etc\hosts" file. Don't forget to save it!
If you don't set it, no page will appear when you access "hogehoge.info"! Here's how to set it up:

After access..

■ Access log 192.168.33.1 - - [14/Oct/2024:08:22:03 +0000] "GET / HTTP/1.1" 301 233 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"

I confirmed that the redirect was actually to "www.hogehoge.info" on Chrome, and I also found in the access log

Part 2: Let's try using AWS ALB

In an AWS environment, it seems that redirection can be set on the load balancer side before the data reaches the server

First, open the AWS console and navigate to the settings screen using the path below

EC2 > Load Balancer > Target ALB > Target port > In the Listener Rules section, click "Add rule"

Once you have made the move, fill in the required fields in steps 1 to 5

Step 1: Add a rule

This is the setting screen for the rule name and tag. Both can be left blank

Step 2: Define the rule conditions

From "Add Condition", select "Rule Condition Type". This is the item to set the access of the transition source

Host Header This corresponds to the
"https:// hogehoge.info , so this time we will enter the original host "hogehoge.info"
path "https://hogehoge.info /test.txt " part.
If access from a specific path is the transition source, it must be specified.
HTTP request method You can specify GET, POST, etc
Source IP You can specify which IP the request is coming from

Step 3: Define rule actions

Next, we will set the transition destination

Action Routing Select "Redirect to a URL"
Redirect to URL Select URI Part
protocol Select any protocol.
In this case, select "HTTP" to verify in a local environment.
port Select any port.
Since "HTTP" was selected as the protocol, enter "80".
Use custom hosts, paths, queries... I'll check
host This corresponds to the
"https:// hogehoge.info , so enter www.hogehoge.info
path corresponds to the
"https://hogehoge.info /test.txt . In this case, we want to apply it to all paths under "www.hogehoge.info", so enter /*
Query - optional refers to
the query parameter
"https://beyondjapan.com/?s=test&post_type=pt-blog . Please enter it if necessary.
status code It is common to use 301 for permanent redirects, and 302 for temporary redirects due to maintenance.
In this case, we will assume a permanent redirect, so we will select "301 - Moved Permanently."

Step 4: Set rule priorities

Set the priority of the redirect setting. When there are multiple redirect settings, the priority is specified by a number to indicate which setting takes priority.
The lower the number, the higher the priority, with 0 being the highest priority.

Since we are only setting one this time, "1" is fine

Things to note when setting:

In addition to the above settings, if you want to set up a redirect from a specific path in "hogehoge.info" (for example, hogehoge.info/test.txt) to another page, give this setting a higher priority

The condition "hogehoge.info/test.txt" is included in the existing condition "hogehoge.info/*" which uses a wildcard, so if the priority is incorrect the redirection will not work properly

Priority 1 "hogehoge.info/test.txt" → "Somewhere"
Priority 2 "hogehoge.info/*" → "www.hogehoge.info/*"

Let's make it so!

Step 5: Review and create

Check that the information you entered is correct and if there are no problems, click Create

Check that it works and if you can confirm that the redirection is successful, then it's OK!

Again, open Notepad with administrator privileges, and don't forget to write and save the local IP and hostname at the bottom of the "C:\windows\system32\drivers\etc\hosts" file!
By the way, this time you will need to write the ALB's public IP, so I will share how to do that as well.

① Go to the details screen of the ALB you are using for verification and copy the "DNS name"

② Open your WSL or Ubuntu and check the IP address using the dig command

dig hoge-test-alb-12345678.ap-northeast-1.elb.amazonaws.com
■Execution result ;; ANSWER SECTION: hoge-test-alb-12345678.ap-northeast-1.elb.amazonaws.com. 60 IN A 12.345.678.90 ←This!

③ Copy the IP address that appears in the ANSWER SECTION and paste it at the bottom of the hosts file as before and save it

summary

The above is how a beginner can set up a redirect

I hope this article will be of some help to beginners... (Prayer)

If you found this article helpful , please give it a like!
6
Loading...
6 votes, average: 1.00 / 16
739
X facebook Hatena Bookmark pocket

The person who wrote this article

About the author

dog