我有一个博客当前托管在我的子域上,我想将其移动到 /blog 子目录。主网站使用CodeIgniter4框架,博客使用Wordpress。它托管在使用 Ubuntu 的 Apache 服务器上。我可以将 blog.example.com 根目录重定向到 www.example.com/blog,但无法将 blog.example.com/123 重定向到 www.example.com/blog/123
这是网站的 .htaccess 文件(我添加的行是
RewriteCond $1 !^(index\.php|blog)
,但我包含了文件的主要部分以供参考:
# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# If you installed CodeIgniter in a subfolder, you will need to
# change the following line to match the subfolder you need.
# http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
# RewriteBase /
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Rewrite "www.example.com -> example.com"
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to the front controller, index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|blog)
RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]
# Ensure Authorization header is passed along
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
这是博客的htaccess:
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{HTTP_HOST} ^blog\.example\.com
RewriteRule ^(.*)$ https://www.example.com/blog/$1 [R=301]
这是 www.example.com 的 Apache 配置文件:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName www.example.com
ServerAlias www.example.com
Protocols h2 http/1.1
DocumentRoot /var/www/example-website/public
<Directory /var/www/example-website/public>
SetEnv website
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Alias /blog "/var/www/example-blog/public_html"
<Directory "/var/www/example-blog/public_html">
SetEnv blog
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/www/example-website/writable/logs/errors.log
CustomLog /var/www/example-website/writable/logs/access.log combined env=website
CustomLog /var/www/example-blog/public_html/wp-content/access.log combined env=blog
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
作为参考,这里是 certbot 生成的 HTTPS 配置文件:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin [email protected]
ServerName www.example.com
ServerAlias www.example.com
Protocols h2 http/1.1
Alias /blog "/var/www/example-blog/public_html"
<Directory "/var/www/example-blog/public_html">
SetEnv blog
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
DocumentRoot /var/www/example-website/public
<Directory /var/www/example-website/public>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/www/example-website/writable/logs/errors.log
CustomLog /var/www/example-website/writable/logs/access.log combined env=website
CustomLog /var/www/example-blog/public_html/wp-content/access.log combined env=blog
RewriteEngine on
# Some rewrite rules in this file were disabled on your HTTPS site,
# because they have the potential to create redirection loops.
# RewriteCond %{SERVER_NAME} =www.example.com
# RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/www.example.com-0001/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com-0001/privkey.pem
</VirtualHost>
</IfModule>
另外,与配置文件相关,有没有办法为博客提供单独的ErrorLog?
问题是博客 htaccess 文件。您需要将重写条件置于 WordPress 添加的指令上方。这是应该如何更新:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.example\.com
RewriteRule (.*) https://www.example.com/blog/$1 [R=301,L]
</IfModule>
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress