Apache設定ですべてのリクエストをindex.htmlにリダイレクトしようとするすべての試みは失敗します。

Apache設定ですべてのリクエストをindex.htmlにリダイレクトしようとするすべての試みは失敗します。

index.htmlのみを要求するAngular SPAがあります。このサーバーへの他のすべての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の尾です。この結果をどのように解釈するのかわかりません。私のWebサイトフォルダのコンテンツには、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

ベストアンサー1

おすすめ記事