[英]How do i make 'index.php' higher priority than 'index.html' with a Shell script
我正在自動化服務器設置並安裝PHP,我需要將index.php
添加到/etc/apache2/mods-enabled/dir.conf
。
文件的目標內容如下所示:
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
我需要在index.html
之前添加index.php
。
它應該看起來像這樣
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
如何使用Shell腳本執行此操作?
您要做的就是使index.php的優先級高於index.html。 這是一件很奇怪的事情,最好用.htaccess之類的東西來完成。 但是,如果您確實想直接在配置文件中進行更改,則可以使用sed
。 在這種情況下,如下所示:
cd /etc/apache2/mods-enabled
sed -e 's/\s*DirectoryIndex.*$/\tDirectoryIndex index\.php <...> index\.htm/' \
dir.conf > dir.conf.tmp && mv dir.conf.tmp dir.conf
您需要確保“ DirectoryIndex”僅出現一次,並用其余的行填充<...>(轉義點:“ index.pl”->“ index \\ .pl”)。
或者您也可以嘗試下一個
<IfModule mod_dir.c>
塊內部進行替換 index.php
index.php
已經存在該行,則對該行進行重新排序 conf="./dir.conf"
err() { echo "$@" >&2; return 1; }
line=$(sed '/<IfModule *mod_dir.c>/,/<\/IfModule>/!d' "$conf" | grep -o -m1 'DirectoryIndex.*')
[[ ! -z "$line" ]] || err "No mod_dir.c" || exit 1
repl=$(echo $(echo DirectoryIndex;echo index.php; tr ' ' '\n' <<<"$line" | grep -Ev 'index\.php|DirectoryIndex'))
sed -i'.bak' "/<IfModule *mod_dir.c>/,/<\/IfModule>/s/$line/$repl/" "$conf"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.