為什麼升級 Ubuntu 18.04.3 後,.htaccess 作用不正常? [論壇 - Ubuntu基本設定]


正在瀏覽:   1 名遊客


 到底部   前一個主題   下一個主題  [無發表權] 請登錄或者註冊



為什麼升級 Ubuntu 18.04.3 後,.htaccess 作用不正常?
會員一級
註冊日期:
2014/10/29 17:22
所屬群組:
已註冊使用者
等級: 1
HP : 0 / 8
MP : 1 / 222
EXP: 34
離線
各位前輩好:
小弟本來是裝 16.04 的版本,一切正常;有一天升級到 18.04 後,架在上面的 WordPress 網站開始不對勁了,只有首頁看得到,子頁面都找不到了(研判是 Rewrite 出了問題)
於是小弟直接重灌 18.04.3 版本,先簡單測試:

1. 裝完 Apache
2. sudo a2enmod rewrite (啟動 rewrite)
3. sudo nano /etc/apache2/sites-available/000-default.conf (修改 apache2 設定)
<VirtualHost *:80>
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>

. . .
</VirtualHost>
4. sudo systemctl restart apache2 (重啟 apache)
5. sudo nano /var/www/html/.htaccess (內容只先打一行 RewriteEngine on)
6. sudo nano /var/www/html/about.html (內容就是簡單的 HTML,例如;Hello)
7. 用瀏覽器輸入 http://網址/about.html 顯示正常
8. 用瀏覽器輸入 http://網址/about "應該"顯示找不到網頁;但它自己對應到 about.html 的內容!
8. 但其實我 .htaccess 檔案中,什麼規則都還沒下!
9. 感覺上,它自己預設了某些 Rewrite 的規則了,是這樣嗎?還是我做錯了什麼?(其他步驟還沒設定,自己就會對應了!)

P.S. 照理說:
/var/www/html/.htaccess 的內容應該是:
RewriteEngine on
RewriteRule ^about$ about.html [NC]

根據上面顯示的規則,以下網址將指向about.html :
http://網址/about.html (因為原始的正確檔名始終有效)
http://網址/about (因為符合規則定義)
http://網址/About (因為規則不區分大小寫)

而以下內容不起作用:
http://網址/about/ (因為規則明確規定在about之後可能沒有任何about,因為$字元出現在about之後
http://http://網址/contact (因為它與規則中的about字串不匹配)

2019/11/26 16:40
應用擴展 工具箱
回覆: 為什麼升級 Ubuntu 18.04.3 後,.htaccess 作用不正常?
會員五級
註冊日期:
2012/4/22 10:50
所屬群組:
已註冊使用者
等級: 37
HP : 0 / 905
MP : 679 / 30238
EXP: 23
離線
請把「MultiViews」拿掉測看看



摘錄 https://httpd.apache.org/docs/2.4/content-negotiation.html#negotiation


The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.




摘錄 https://httpd.apache.org/docs/2.4/mod/mod_negotiation.html#multiviews



A Multiviews search is enabled by the Multiviews Options. If the server receives a request for /some/dir/foo and /some/dir/foo does not exist, then the server reads the directory looking for all files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements, and returns that document.



## 相關網址

* https://httpd.apache.org/docs/2.4/mod/core.html#options


## 版本資訊

執行


$ apache2 -v



顯示


Server version: Apache/2.4.29 (Ubuntu)
Server built: 2019-09-16T12:58:48


2019/11/26 19:00
應用擴展 工具箱
回覆: 為什麼升級 Ubuntu 18.04.3 後,.htaccess 作用不正常?
會員一級
註冊日期:
2014/10/29 17:22
所屬群組:
已註冊使用者
等級: 1
HP : 0 / 8
MP : 1 / 222
EXP: 34
離線
samwhelp 您好!

先謝謝您的回覆!
想確認一下
是 sudo nano /etc/apache2/sites-available/000-default.conf 內容改為:

<VirtualHost *:80>
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

我會試試看,謝謝您!

2019/11/27 1:19
應用擴展 工具箱
回覆: 為什麼升級 Ubuntu 18.04.3 後,.htaccess 作用不正常?
會員一級
註冊日期:
2014/10/29 17:22
所屬群組:
已註冊使用者
等級: 1
HP : 0 / 8
MP : 1 / 222
EXP: 34
離線
samwhelp 您好!

請問,在 16.02 版本中,沒有 MultiViews 功能嗎?
因為我之前並沒有動到 MultiViews 這個參數...

2019/11/27 1:21
應用擴展 工具箱
回覆: 為什麼升級 Ubuntu 18.04.3 後,.htaccess 作用不正常?
會員五級
註冊日期:
2012/4/22 10:50
所屬群組:
已註冊使用者
等級: 37
HP : 0 / 905
MP : 679 / 30238
EXP: 23
離線
TeRRy Liu 寫到:
samwhelp 您好!

請問,在 16.02 版本中,沒有 MultiViews 功能嗎?
因為我之前並沒有動到 MultiViews 這個參數...


我的環境是「Ubuntu 18.04」,所以我也不曉得耶,
剛查了一下

* https://packages.ubuntu.com/xenial/apache2 (16.04) (apache2: 2.4.18)
* https://packages.ubuntu.com/bionic/apache2 (18.04) (apache2: 2.4.29)

哇,都是「2.4」,這我真的就不曉得了 Orz...

阿,突然想到,也許原本您在「16.04」沒有啟動「negotiation」這個模組,這也是一種可能性。

根據上面「#2」提到的文件「https://httpd.apache.org/docs/2.4/mod/mod_negotiation.html#multiviews」,
可以看到「Multiviews」應該是「Apache Module mod_negotiation」提供的



我在「18.04」測試


$ dpkg -L apache2 | grep negotiation



顯示


/etc/apache2/mods-available/negotiation.conf
/etc/apache2/mods-available/negotiation.load



執行


$ ls /etc/apache2/mods-enabled/ -1 | grep negotiation



顯示


negotiation.conf
negotiation.load



表示「negotiation」在我的環境是有啟用的

或是執行


$ find /etc/apache2/mods-enabled/ | grep negotiation



顯示


/etc/apache2/mods-enabled/negotiation.load
/etc/apache2/mods-enabled/negotiation.conf



執行


file $(find /etc/apache2/mods-enabled/ | grep negotiation)



顯示


/etc/apache2/mods-enabled/negotiation.load: symbolic link to ../mods-available/negotiation.load
/etc/apache2/mods-enabled/negotiation.conf: symbolic link to ../mods-available/negotiation.conf



這也是使用「sudo a2enmod」的原理,
啟用模組,在「/etc/apache2/mods-enabled/」就有。
停用模組,在「/etc/apache2/mods-enabled/」就沒有。

執行下面指令,了解「a2enmod」放在那個路徑。


$ which a2enmod



顯示


/usr/sbin/a2enmod



執行下面指令,了解「/usr/sbin/a2enmod」。


file $(which a2enmod)



顯示


/usr/sbin/a2enmod: Perl script text executable



因為「/usr/sbin/a2enmod」是「Perl script」,
所以就可以執行下面指令,直接觀看「/usr/sbin/a2enmod」的內容,來探索了。


$ less /usr/sbin/a2enmod



不過您有興趣深究,再去探索「/usr/sbin/a2enmod」。
我只是稍微紀錄,提供一下探索的起點。
也可以執行「man a2enmod」,閱讀說明。

================================================================================

TeRRy Liu 寫到:
samwhelp 您好!

先謝謝您的回覆!
想確認一下
是 sudo nano /etc/apache2/sites-available/000-default.conf 內容改為:

<VirtualHost *:80>
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

我會試試看,謝謝您!


是,您可以測測看,看看在您的環境是否OK?

我之前的回覆「#2」,也是複製您在「#1」提到的,
然後我用「土法煉鋼」的方式,採用「刪除法」測試,
一次測一個「因素」,也就是一次拿掉某一個「因素」來測試,
在把「MultiViews」拿掉的時候,
我測試到的結果,就會看到在「#1」您提到的「http://localhost/about」是「404」了,

然後才使用「apache multiviews」當關鍵字查詢,才找到「#2」提到的說明連結。

您可以參考我在「#2」摘錄的官方文件說明。
我有附連結,您也可以回到官方網站觀看「MultiViews」的說明。

================================================================================

以上提供參考

報告完畢


2019/11/27 8:13
應用擴展 工具箱
回覆: 為什麼升級 Ubuntu 18.04.3 後,.htaccess 作用不正常?
會員一級
註冊日期:
2014/10/29 17:22
所屬群組:
已註冊使用者
等級: 1
HP : 0 / 8
MP : 1 / 222
EXP: 34
離線
samwhelp 前輩:

謝謝您!我在想,除了重裝18.04測試外,我是不是要再弄一台VM,裝16.04試試,是不是16.04的沒有MultiViews!

真的很謝謝您的回應!

2019/11/27 22:57
應用擴展 工具箱
回覆: 為什麼升級 Ubuntu 18.04.3 後,.htaccess 作用不正常?
會員三級
註冊日期:
2017/3/23 10:15
所屬群組:
已註冊使用者
等級: 10
HP : 0 / 247
MP : 46 / 4699
EXP: 88
離線

2019/11/28 5:37
應用擴展 工具箱
回覆: 為什麼升級 Ubuntu 18.04.3 後,.htaccess 作用不正常?
會員一級
註冊日期:
2014/10/29 17:22
所屬群組:
已註冊使用者
等級: 1
HP : 0 / 8
MP : 1 / 222
EXP: 34
離線
Wow... 呆丸廖添丁前輩,我沒那個能力啦!
謝謝您哦!

2019/11/29 10:28
應用擴展 工具箱


 [無發表權] 請登錄或者註冊


可以查看帖子.
不可發帖.
不可回覆.
不可編輯自己的帖子.
不可刪除自己的帖子.
不可發起投票調查.
不可在投票調查中投票.
不可上傳附件.
不可不經審核直接發帖.