你好,我试图从apache迁移到nginx,但是我坚持URL重写。
这是用于NUXT SPA应用程序的。
对于apache,我使用.htaccess来重写url。
但不幸的是,nginx上没有htaccess ..。
以下是预期的结果:
website.com/ -> /index.htmlwebsite.com/ch/en注意到有很多不同的国家和语言,所以我认为手工编写所有重写都不是一种选择。
我的文件夹结构如下:
dist/
.htaccess
index.html
_nuxt/
(nuxt generated files)
data/
(static file like image, json, etc...)
ch/
fr/
.htaccess
index.html
de/
.htaccess
index.html
be/
fr/
.htaccess
index.html
...我的根.htaccess包含:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>在我的子文件夹中,例如/ch/en
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ch/en
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /ch/en/index.html [L]
</IfModule>目前,我尝试在nginx.conf上遵循以下规则
...
location / {
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /$1/$2/ {
index index.html index.htm;
try_files $uri $uri/ /$1/$2/index.html;
}
...但是,url rewrinting并没有像预期的那样工作:
例如,/ch/en很好地显示/ch/en/index.html
但是/ch/en/mypage显示的是/index.html而不是/ch/en/index.html
那么,我如何使重写像/* -> /index.html和/$1/$2/ -> /$1/$2/index.html on nginx.conf?
发布于 2020-06-12 10:53:34
试试这个:
error_page 404 /index.html;
location / {
index index.html index.htm;
try_files $uri /index.html;
}
location ~ ^(/[^/]+/[^/]+) {
index index.html index.htm;
try_files $uri $1/index.html =404;
}https://stackoverflow.com/questions/62342338
复制相似问题