[24th grade] Beginner innocently tried setting redirect [Apache] [AWS]
table of contents
Hello. I'm Inu, a 24-year-old apprentice infrastructure engineer with a liberal arts background.
These days I want to be able to move my head more than my eyes.
This time, I will share the results of what I learned about redirect settings as an amateur in a primitive, physical, and fetish way.
What is a redirect?
First, let's take a look at Wikipedia's explanation.
Redirect (forwarding) is a function that redirects you to another page when you link to an article. Additionally, such pages are called redirect pages.
That's exactly what it is.
Have you ever been automatically redirected to a new page when you access your old homepage?
That is exactly a "redirect" is.
Let's try redirect settings
How do you set up redirects?
So, this time I would like to redirect from "hogehoge.info" to "www.hogehoge.info" by setting it on the server side and using AWS's ALB
Verification environment
the setting method on the server side , I used the following verification environment to set it up.
Virtualbox: Version 7.0.20
Vagrant: version 2.4.1
Click here to learn how to launch a verification environment with Vagrant ↓
Vagrant box: AlmaLinux 9
Apache: version 2.4.57
Part 1: Let's configure it on the server side
If you want to configure it on the server side, try adding an alias to the virtual host's conf file + adding a description to the virtual host's ".htaccess".
① Add an alias to the virtual host's conf file
First, check the list of virtual hosts using the command below.
httpd -S
In the execution results, you can see 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" ~abbreviation~
Now, let's look at the conf file for "www.hogehoge.info" using the less command.
less /etc/httpd/conf.d/hogehoge.conf
The default settings are the same, so we will add "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>
Edit the conf file for "www.hogehoge.info" using the vi command.
vi /etc/httpd/conf.d/hogehoge.conf
Go to edit mode, then under ServerName, add "ServerAlias".
<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 from edit mode with esc, and save and exit with :wq.
After confirming that "ServerAlias" has been added correctly using the cat command, move on to the next step.
② Describe in ".htaccess" of virtual host
Next, let's create ".htaccess".
First, move to the document root of "www.hogehoge.info".
cd /var/www/vhosts/hogehoge.info/public_html
Once moved, create ".htaccess" inside public_html
vi .htaccess
When you open the vi screen, a brand new editing screen will appear, so add the following information.
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 them means.
Rewrite Engine on | Enable URL rewriting |
RewriteCond %{HTTP_HOST} ^hogehoge\.info [NC] | Define the condition so that the variable representing the host name of the URL = 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, returns status code 301, and redirects them. |
Hmm, that's difficult.
This article was very easy to understand.
After editing, reload the settings using the command below.
systemctl reload httpd
Finally, try actually accessing "hogehoge.info" and see if you are redirected to "www.hogehoge.info".
Don't forget to open Notepad with administrator privileges and write and save the local IP and host name at the bottom of the "C:\windows\system32\drivers\etc\hosts" file!
If you don't set it up, no page will appear even if you access "hogehoge.info"! ! The setting method is like this↓
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"
We confirmed that we were actually redirected to "www.hogehoge.info" on Chrome, and we also found in the access log
Part 2: Let's use AWS's ALB
In the AWS environment, it seems that redirect settings can be configured on the load balancer side before reaching the server.
First, open the AWS console screen and move to the settings screen using the path below.
EC2 > Load balancer > Target ALB > Target port > "Add rule" in the listener rules section
Once you are able to move, fill in the necessary items in steps 1 to 5.
Step 1: Add a rule
This is the rule name and tag setting screen. There is no problem if both are blank.
Step ② Define rule conditions
From "Add Condition", select "Rule Condition Type". This item is used to configure access for the transition source.
host header | This corresponds to the "https:// hogehoge.info , so this time enter "hogehoge.info" which is the source host. |
path | This corresponds to the "https://hogehoge.info /test.txt . It must be specified if access from a specific path is the transition source. |
HTTP request method | GET, POST, etc. can be specified. |
Source IP | You can specify which IP the request comes from |
Step ③ Define rule action
Next, we will set the transition destination.
Routing actions | Select "Redirect to URL" |
redirect to URL | Select "URI part" |
protocol | Select any protocol. This time, select "HTTP" to verify in the local environment. |
port | Select any port. Since we selected "HTTP" as the protocol this time, enter "80". |
Use custom hosts, paths, queries... | I'll check |
host | It corresponds to the "https:// hogehoge.info , so enter www.hogehoge.info |
path | corresponds to the "https://hogehoge.info /test.txt . This time, we want to apply it to all paths under "www.hogehoge.info", so enter /* |
Query - optional | This is the query parameter, which the "https://beyondjapan.com/ ?s=test&post_type=pt-blog " part. Please enter it if necessary. |
status code | It seems common to use 301 for permanent redirects, and 302 for temporary maintenance redirects.In this case, we assume that it is a permanent redirect, and will say "301 - Permanently moved." select |
Step ④ Set rule priority
Set the priority of redirect settings. Priority is a numerical value that specifies which setting takes priority when multiple redirect settings exist.
The numbers are prioritized from lowest to highest, with 0 being the most preferred number.
This time, we will only set one, so "1" is fine.
Points to note when setting:
Apart from the above settings, if you want to add a setting to redirect to another page from a specific path of "hogehoge.info" (for example, hogehoge.info/test.txt, etc.), make this setting higher in priority. Sho.
Since the condition "hogehoge.info/test.txt" is included in the existing condition "hogehoge.info/*" using a wildcard, redirection will not work properly if the priority order is incorrect.
Priority 1 "hogehoge.info/test.txt" → "Somewhere"
Priority 2 "hogehoge.info/*" → "www.hogehoge.info/*"
Let's make it so!
Step ⑤ Confirm and create
Check that there are no problems with the entered information, and if there are no problems, press Create.
If you check the operation and confirm that the redirection was successful, it is OK!
Again, don't forget to open Notepad with administrator privileges and write and save the local IP and host name at the bottom of the "C:\windows\system32\drivers\etc\hosts" file!
By the way, this time we need to enter the ALB's public IP, so I will also share how to do that.
① Go to the details screen of the ALB used for verification and copy the "DNS name".
② Open your WSL or Ubuntu and check the IP 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 that appears in the ANSWER SECTION, paste it at the bottom of the hosts file, and save it as before.
summary
The above was about beginners trying to set up redirects.
I hope this article will be of some help to beginners, (gassho)