微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当 Wordpress 在子文件夹中时加载 URL 时正斜杠丢失

如何解决当 Wordpress 在子文件夹中时加载 URL 时正斜杠丢失

问题来了: 我已经在一个子目录中设置了 wordpress,当我去:

example.com/blog -> example.com/blog  //and it works perfectly

当我点击博客文章时,它会这样做:

example.com/blog/some-post -> example.com/blogsomepost //it removes the forward slash after blog/

如何在 URL 中保留正斜杠并避免重定向到 /blogsomepost ?

我目前的设置是这样的:

我有两台服务器:

1. 在我放置的服务器和 Nginx 上的某个应用程序上:

location /blog {
    proxy_ssl_server_name on;
    proxy_set_header X-Forwarded-Host $host:$server_port;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Proxy-Forward True;
    proxy_pass https://blog.example.com/; // where by blog is set-up

}

2. 第二个服务器是 wordpress 安装,我的博客目录中有 apache 和 .htaccess:

  1. /etc/apache2/sites-enabled/mysite.conf
<VirtualHost *:80>
    ServerName blog.example.com
    ServerAlias www.blog.example.com
    DocumentRoot /var/www/blog.example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    LogLevel alert rewrite:trace6

#    ProxyPass "/" "https://example.com/blog/"
#    ProxyPassReverse "/" "https://example.com/blog/"

    <Directory /var/www/blog.example.com/>
       AllowOverride All
    </Directory>

</VirtualHost>

  1. /var/www/blog.example.com/.htaccess
<IfModule mod_rewrite.c>
 RewriteBase /blog
 RewriteRule ^index\.PHP$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*) "/index.PHP"  [L]
</IfModule>
  1. /var/www/blog.example.com/wp-config.php
define('WP_SITEURL','https://example.com/blog/');
define('WP_HOME','https://example.com/blog/');

测试

当我这样做时:

curl -v -L https://example.com/blog/sompepost/

我明白了:

* Using HTTP2,server supports multi-use
* Connection state changed (HTTP/2 confirmed)
> GET /blog/sompepost/ HTTP/2
> Host: example.com
> user-agent: curl/7.68.0
> accept: */*
> 
* TLSv1.3 (IN),TLS handshake,Newsession Ticket (4):
< HTTP/2 301 
< content-type: text/html; charset=UTF-8
< location: http://example.com/blogsompepost/
< x-pingback: https://example.com/blog/xmlrpc.PHP
< x-redirect-by: wordpress
< cf-cache-status: DYNAMIC
< server: cloudflare
< 
* Connection #0 to host example.com left intact
* Issue another request to this URL: 'http://example.com/blogsompepost/'
> GET /blogsompepost/ HTTP/1.1

一般来说,页面加载正常,并按预期显示所有内容,但 URL 错误

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。