htaccess 在多语言网站上显示主网站而不是子文件夹
我正在开发一个支持多语言的网站。 URL 的格式如下:
https://example.com/gb/en/
https://example.com/fr/fr/
gb
指国家/地区,en
指语言。 .htaccess
文件处理这些变量并从文件加载特定于语言的数据。
我试图在子文件夹中复制该网站,并使其与原始网站完全独立地工作。
https://example.com/promo/gb/en/
https://example.com/promo/fr/fr/
我的 .htaccess
文件如下所示:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
ErrorDocument 404 /404.php
#force https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteRule \.js$ /sprocketize.php [QSA,L,NC]
RewriteRule ^(admin) - [L,NC]
RewriteRule ^(app) - [L,NC]
RewriteRule ^(ws) - [L,NC]
RewriteRule ^(opia) - [L,NC]
RewriteRule ^promo-select(/)?$ /promo-select.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule ^([a-z]{2})/(.+)/$ http://%{HTTP_HOST}/$1/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2})/([a-z]{2})(?:/(.*)|)$ /$1/$3?lang=$2 [NC,QSA,PT]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2})(?:/(.*)|)$ /$2?country=$1 [NC,QSA,PT]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /$2?keyword=$1 [NC,QSA,PT]
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /$1.php [NC,QSA]
我尝试更改
RewriteRule ^([^/]+)/(.*)$ /$2?keyword=$1 [NC,QSA,PT ]
到
RewriteRule ^promo/([^/]+)/(.*)$ /promo/$2?keyword=$1 [NC,QSA,PT]
等,但是它要么正在加载原始站点,要么显示“找不到页面”错误。我还尝试添加 RewriteBase /promo
但它只是加载了主站点。我怀疑我做了一些明显/愚蠢的错误。谢谢。
UPDATE
我应该说,子文件夹中的文件与根文件夹相同,包括 .htaccess
文件。有 2 个 .htaccess
文件,1 个位于根文件夹中,1 个位于子文件夹中。我一直在编辑子文件夹中的那个。
I am working on a website with multi-language support. The URLs are in the format:
https://example.com/gb/en/
https://example.com/fr/fr/
gb
refers to the country, en
refers to the language. The .htaccess
file handles these variables and loads the language specific data from a file.
I am trying to duplicate this site in a subfolder, and have it working completely seperately to the original site.
https://example.com/promo/gb/en/
https://example.com/promo/fr/fr/
My .htaccess
file looks like this:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
ErrorDocument 404 /404.php
#force https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteRule \.js$ /sprocketize.php [QSA,L,NC]
RewriteRule ^(admin) - [L,NC]
RewriteRule ^(app) - [L,NC]
RewriteRule ^(ws) - [L,NC]
RewriteRule ^(opia) - [L,NC]
RewriteRule ^promo-select(/)?$ /promo-select.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule ^([a-z]{2})/(.+)/$ http://%{HTTP_HOST}/$1/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2})/([a-z]{2})(?:/(.*)|)$ /$1/$3?lang=$2 [NC,QSA,PT]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2})(?:/(.*)|)$ /$2?country=$1 [NC,QSA,PT]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /$2?keyword=$1 [NC,QSA,PT]
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /$1.php [NC,QSA]
I've tried changing
RewriteRule ^([^/]+)/(.*)$ /$2?keyword=$1 [NC,QSA,PT]
to
RewriteRule ^promo/([^/]+)/(.*)$ /promo/$2?keyword=$1 [NC,QSA,PT]
etc., but it's either loading the original site or showing a 'page not found' error. I've also tried adding RewriteBase /promo
but it just loaded the main site. I suspect I'm doing something obvious/stupid wrong. Thanks.
UPDATE
I should have said, the files in the subfolder are the same as the root folder, including the .htaccess
file. There are 2 .htaccess
files, 1 in the root, 1 in the subfolder. I've been editing the one in the subfolder.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重要
使用 .htaccess 通常在编辑文件后清除浏览器缓存很重要。我遇到了一些问题,
您是否尝试过重写文件夹的网址?
请注意尾随的“/”,我认为应该在顶部
我使用它并且它适用于我的服务器...
请尝试这个并告诉我们会发生什么:
1. 更改
为:
如果以上不起作用,请尝试:
更改
为:
Important
While working with .htaccess is usually important to clear browser cache after editing the file. I've had some problems with this
Have you tried to rewrite urls to your folder?
Notice the trailing '/' and this, I think, should be on top
I use this and it works with my server...
Please try this and tell us what happens:
1. Change
to:
If above don't work, try:
Change
to: