这个问题有历史价值,所以我稍微更新一下。这是 Google 中“webpack-dev-server wordpress redirect”的最高结果。虽然已接受的解决方案适用于 Webpack 2,但它可能不再适用。如果没有的话可以参考我的WordPress 主题库 https://github.com/k1sul1/wordpress-theme-base,它是使用 Webpack 4 构建的。
首先,这与当 Webpack Dev Server 代理时,Wordpress 重定向到本地主机而不是虚拟主机 https://stackoverflow.com/questions/35432990/wordpress-redirecting-to-localhost-instead-of-virtual-host-when-being-proxied-by。我面临着类似的问题,但唯一的解决方案并没有真正为我做任何事情。
我在 Vagrant 开发机器中运行 WordPress 4.7,它响应http://wordpress.local http://wordpress.local就像它应该的那样。之前我使用 Browsersync 来监视我的文件并触发刷新,这按预期工作:browser-sync start --proxy 'https://wordpress.local' --files '**/dist/js/*.js, **/*.css, **/*.php'
.
但是,使用 webpack-dev-server 我无法复制该行为。这是应该发生的事情。
- 服务器启动于
https://localhost:9000
- 导航至
https://localhost:9000
应该向我展示与导航到相同的页面https://wordpress.local
,没有任何重定向。网站按原样运行https://wordpress.local
,但网址是https://localhost:9000
.
- 发生变化,页面重新加载。
相反,这种情况发生了。
- 导航至
https://localhost:9000
重定向 me to https://wordpress.local
使用 301。我已禁用规范重定向remove_filter('template_redirect', 'redirect_canonical');
但没有帮助。
- 导航至
https://localhost:9000/404
向我展示一个由我的主题提供的 404 页面。不会发生重定向。
- 导航至
https://localhost:9000/existing-page/
重定向 me to https://localhost/existing-page/
与301。
到底发生了什么事?我已将问题范围缩小到 WordPress,因为代理非 WordPress 目录按预期工作:
直接,$_SERVER的内容:https://gist.github.com/k1sul1/0aff7ba905464ca7852f2ce00b459922 https://gist.github.com/k1sul1/0aff7ba905464ca7852f2ce00b459922
代理,$_SERVER 的内容:https://gist.github.com/k1sul1/f090aa103dc3a3cb0b339269560ac19d https://gist.github.com/k1sul1/f090aa103dc3a3cb0b339269560ac19d
我尝试过使用标题等,但没有运气。这是我的 webpack.config.js 的样子:
const path = require('path');
const url = 'https://wordpress.local/';
const themeDir = '/wp-content/themes/themename/';
module.exports = {
entry: './src/js/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: url
},
devServer: {
historyApiFallback: true,
compress: true,
port: 9000,
https: url.indexOf('https') > -1 ? true : false,
publicPath: themeDir,
proxy: {
'*': {
'target': url,
'secure': false
},
// '/': { // This doesn't do much.
// target: url,
// secure: false
// }
},
}
};
TL;DR:如何使用 webpack-dev-server 复制 Browsersync 行为而不让 WordPress 发疯?