我有一个 Angular SPA,应该只请求index.html。对此服务器的所有其他 URL 请求均由 SPA 在内部处理。我想将所有 404 错误重定向到index.html。
[更新:这篇文章描述了我如何尝试使用 mod_rewrite 来实现此目的。也许这不是我应该做的。]
我是 Apache 配置新手。我在网上找到的每个解决方案都失败了。我能够成功添加重写以将 http 流量重定向到 https。所以我认为 mod_rewrite 应该正确设置。但我所有其他重写尝试都失败了。我可能犯了一些明显的新手错误。
我正在运行: 服务器版本:Apache/2.4.41 (Ubuntu) 服务器构建:2021-10-14T16:24:43
编辑配置文件后,我总是运行“sudo systemctl reload apache2.1”。
这是我的虚拟主机 govmeeting.org.conf 的配置文件:
<Directory /var/www/html>
Require all granted
</Directory>
<VirtualHost *:80>
ServerName govmeeting.org
ServerAlias www.govmeeting.org
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# This re-directs http traffic to https. This works.
RewriteEngine on
RewriteCond %{SERVER_NAME} =govmeeting.org [OR]
RewriteCond %{SERVER_NAME} =www.govmeeting.org
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
# This was one solution I found to redirect all 404 errs to the main domain.
# It does NOT work. I still get 404 errs for govmeeting.org/somepage.
# ErrorDocument 404 https://govmeeting.org/
# This is another solution that I found that does NOT work:
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule . /index.html [L]
#
# Here is another suggestion that does NOT work:
# RewriteBase /
# RewriteRule ^index\.html$ - [L]
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule . /index.html [L]
#
# In this, I tried to redirect "somepage" to index.html. Does NOT work.
# I tried with and without a second "RewriteEngine on", because the
# suggestion was using it twice.
# RewriteEngine on
# RewriteRule ^somepage$ index.html [NC]
#
# Here I added "/" before each file name. Does NOT work.
# RewriteEngine on
# Redirect /x.html /index.html
</VirtualHost>
更新:我设置日志记录:
LogLevel trace6 rewrite_module:trace6
我编辑了我的配置以仅具有此重定向:
RewriteEngine on
RewriteRule ^home.html$ index.html [NC]
然后,我从浏览器发送了对“govmeeting.org/dashboard”、“.../xxx.txt”和“.../home.html”的请求。
这是 access.log 的尾部:
114.119.156.56 - - [04/Dec/2021:02:03:03 +0000] "GET /robots.txt HTTP/1.1" 404 493 "-" "Mozilla/5.0 (compatible;PetalBot;+https://webmaster.petalsearch.com/site/petalbot)"
67.80.142.21 - - [04/Dec/2021:02:07:48 +0000] "GET /dashboard HTTP/1.1" 404 5729 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"
67.80.142.21 - - [04/Dec/2021:02:08:08 +0000] "-" 408 5213 "-" "-"
188.166.170.135 - - [04/Dec/2021:02:12:50 +0000] "GET /currentsetting.htm HTTP/1.1" 404 5607 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"
188.166.170.135 - - [04/Dec/2021:02:12:50 +0000] "GET / HTTP/1.0" 400 624 "-" "-"
67.80.142.21 - - [04/Dec/2021:02:19:58 +0000] "GET /xxx.txt HTTP/1.1" 404 5729 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"
67.80.142.21 - - [04/Dec/2021:02:20:18 +0000] "-" 408 5213 "-" "-"
67.80.142.21 - - [04/Dec/2021:02:25:06 +0000] "GET /home.html HTTP/1.1" 404 5729 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"
67.80.142.21 - - [04/Dec/2021:02:25:26 +0000] "-" 408 5213 "-" "-"
这是 error.log 的尾部。我不知道如何解释这些结果。我的网站文件夹的内容仅包含一个index.html 和一组javascript 文件。 (没有“robots.txt”等)。它是否试图重写这些请求?
[Sat Dec 04 02:03:03.013635 2021] [rewrite:trace2] [pid 4283] mod_rewrite.c(483): [client 114.119.156.56:18482] 114.119.156.56 - - [govmeeting.org/sid#7f337385b810][rid#7f337391a0a0/initial] init rewrite engine with requested uri /robots.txt
[Sat Dec 04 02:03:03.013654 2021] [rewrite:trace3] [pid 4283] mod_rewrite.c(483): [client 114.119.156.56:18482] 114.119.156.56 - - [govmeeting.org/sid#7f337385b810][rid#7f337391a0a0/initial] applying pattern '/^home.html$' to uri '/robots.txt'
[Sat Dec 04 02:03:03.013702 2021] [rewrite:trace1] [pid 4283] mod_rewrite.c(483): [client 114.119.156.56:18482] 114.119.156.56 - - [govmeeting.org/sid#7f337385b810][rid#7f337391a0a0/initial] pass through /robots.txt
[Sat Dec 04 02:28:32.122092 2021] [rewrite:trace2] [pid 4566] mod_rewrite.c(483): [client 167.248.133.59:50802] 167.248.133.59 - - [35.211.93.45/sid#7fd23b007810][rid#7fd23ca000a0/initial] init rewrite engine with requested uri /
[Sat Dec 04 02:28:32.122099 2021] [rewrite:trace3] [pid 4566] mod_rewrite.c(483): [client 167.248.133.59:50802] 167.248.133.59 - - [35.211.93.45/sid#7fd23b007810][rid#7fd23ca000a0/initial] applying pattern '^home.html$' to uri '/'
[Sat Dec 04 02:28:32.122175 2021] [rewrite:trace1] [pid 4566] mod_rewrite.c(483): [client 167.248.133.59:50802] 167.248.133.59 - - [35.211.93.45/sid#7fd23b007810][rid#7fd23ca000a0/initial] pass through /
[Sat Dec 04 02:28:32.122306 2021] [rewrite:trace2] [pid 4566] mod_rewrite.c(483): [client 167.248.133.59:50802] 167.248.133.59 - - [35.211.93.45/sid#7fd23b007810][rid#7fd23b1090a0/subreq] init rewrite engine with requested uri /index.html
[Sat Dec 04 02:28:32.122311 2021] [rewrite:trace3] [pid 4566] mod_rewrite.c(483): [client 167.248.133.59:50802] 167.248.133.59 - - [35.211.93.45/sid#7fd23b007810][rid#7fd23b1090a0/subreq] applying pattern '^home.html$' to uri '/index.html'
[Sat Dec 04 02:28:32.122316 2021] [rewrite:trace1] [pid 4566] mod_rewrite.c(483): [client 167.248.133.59:50802] 167.248.133.59 - - [35.211.93.45/sid#7fd23b007810][rid#7fd23b1090a0/subreq] pass through /index.html
[Sat Dec 04 02:28:32.195319 2021] [rewrite:trace2] [pid 4567] mod_rewrite.c(483): [client 167.248.133.59:40432] 167.248.133.59 - - [35.211.93.45/sid#7fd23b007810][rid#7fd23ca000a0/initial] init rewrite engine with requested uri /
[Sat Dec 04 02:28:32.195325 2021] [rewrite:trace3] [pid 4567] mod_rewrite.c(483): [client 167.248.133.59:40432] 167.248.133.59 - - [35.211.93.45/sid#7fd23b007810][rid#7fd23ca000a0/initial] applying pattern '^home.html$' to uri '/'
[Sat Dec 04 02:28:32.195342 2021] [rewrite:trace1] [pid 4567] mod_rewrite.c(483): [client 167.248.133.59:40432] 167.248.133.59 - - [35.211.93.45/sid#7fd23b007810][rid#7fd23ca000a0/initial] pass through /
[Sat Dec 04 02:28:32.195467 2021] [rewrite:trace2] [pid 4567] mod_rewrite.c(483): [client 167.248.133.59:40432] 167.248.133.59 - - [35.211.93.45/sid#7fd23b007810][rid#7fd23b1090a0/subreq] init rewrite engine with requested uri /index.html
[Sat Dec 04 02:28:32.195472 2021] [rewrite:trace3] [pid 4567] mod_rewrite.c(483): [client 167.248.133.59:40432] 167.248.133.59 - - [35.211.93.45/sid#7fd23b007810][rid#7fd23b1090a0/subreq] applying pattern '^home.html$' to uri '/index.html'
[Sat Dec 04 02:28:32.195476 2021] [rewrite:trace1] [pid 4567] mod_rewrite.c(483): [client 167.248.133.59:40432] 167.248.133.59 - - [35.211.93.45/sid#7fd23b007810][rid#7fd23b1090a0/subreq] pass through /index.html