我有一个虚拟主机apache配置文件,我想用shell脚本改变标签<VirtualHost *:80></VirtualHost>
内的所有内容。
我在virtualhost.conf:
<VirtualHost *:80>
ServerName mysite.foo.bar
DocumentRoot /var/www/html/src/
<Directory /var/www/html/src/>
Require all granted
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?m=$1 [QSA]
DirectoryIndex index.php
</Directory>
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName mysite.foo.bar
DocumentRoot /var/www/html/src/
<Directory /var/www/html/src/>
Require all granted
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?m=$1 [QSA]
DirectoryIndex index.php
</Directory>
SSLCertificateFile /etc/foo/bar/fullchain.pem
SSLCertificateKeyFile /etc/foo/bar/privkey.pem
Include /etc/foo/options-ssl-apache.conf
</VirtualHost>
</IfModule>
并且,使用shell脚本,我想将其更改为:
<VirtualHost *:80>
ServerName mysite.foo.bar
Redirect permanent / https://mysite.foo.bar/
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName mysite.foo.bar
DocumentRoot /var/www/html/src/
<Directory /var/www/html/src/>
Require all granted
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?m=$1 [QSA]
DirectoryIndex index.php
</Directory>
SSLCertificateFile /etc/foo/bar/fullchain.pem
SSLCertificateKeyFile /etc/foo/bar/privkey.pem
Include /etc/foo/options-ssl-apache.conf
</VirtualHost>
</IfModule>
换句话说,我想将端口80上的所有流量重定向到端口443,但我需要在我的进程中自动进行此更改。
使用GNUsed
:
sed -E '
/^s*<VirtualHosts.*:80>s*$/,/^s*</VirtualHost>s*$/{
/^s*</?VirtualHost.*>s*$/b
/^s*ServerNames+(S+).*/!d
s%%&n Redirect permanent / https://1/%
}' virtualhost.conf
With awk:
awk 'BEGIN { mark=1 } /^<VirtualHost *:80>/ { print;printf "tServerName mysite.foo.barntRedirect permanent / https://mysite.foo.bar/n";mark=0;next } /</VirtualHost>/ { mark=1 }mark' file
解释:
awk 'BEGIN {
mark=1 # Set a print marker (mark) to 1 before file processing
}
/^<VirtualHost *:80>/ { # Process when we see a Virtual Host on 80
print; # Print the line
printf "tServerName mysite.foo.barntRedirect permanent / https://mysite.foo.bar/n"; # Print the additional text
mark=0; # Set print marker to 0
next # Skip to the next line
}
/</VirtualHost>/ { # Process when we see a closing Virtual Host tag
mark=1 # Set print marker to 1
}mark' file # Use print marker to distinguish what should and shouldn't print