apache .htaccess 重写到默认语言文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1689283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:22:55  来源:igfitidea点击:

.htaccess rewrite to default language folder?

apachemod-rewrite

提问by neezer

I have my site broken down into several folders by language:

我的网站按语言分为几个文件夹:

/
  /en/
    index.php
    about-us.php
    faq.php
    ...

  /fr/
    index.php
    about-us.php
    faq.php
    ...

  ...
  etc.

I'd like to have a rewrite rule that automatically rewrites to the enfolder if somebody tried to enter mydomain.com/about-us.php.

我想要一个重写规则,en如果有人试图输入mydomain.com/about-us.php.

FYI, I also already have a rewrite rule in place that removes the extension, so really I want to make sure that mydomain.com/about-usrewrites to mydomain.com/en/about-us. Here's my existing rewrite rule that does this:

仅供参考,我也已经制定了删除扩展名的重写规则,所以我真的想确保mydomain.com/about-us重写为mydomain.com/en/about-us. 这是我现有的重​​写规则:

# allows for extension-agnostic urls 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ .php

That make sense? Anyone help me out here?

有道理?有人帮我吗?

EDIT:

编辑:

@Gumbo -

@秋葵 -

Here's what my .htaccessfile looks like (this is all that's in it):

这是我的.htaccess文件的样子(这就是其中的全部内容):

RewriteEngine On

# defaults to the english site
RewriteRule !^[a-z]{2}/ /en%{REQUEST_URI} [L,R=301]

# allows for extension-agnostic urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ .php

This file is in the root of my website (not in a language folder). I've reloaded my webserver, just to be on the safe side, and I still get this error message when I try to go to mydomain.com/about-us:

该文件位于我网站的根目录中(不在语言文件夹中)。我已经重新加载了我的网络服务器,只是为了安全起见,当我尝试转到时仍然收到此错误消息mydomain.com/about-us

Not Found

The requested URL /about-us was not found on this server.

... where mydomain.com/en/about-usworks just fine.

...哪里mydomain.com/en/about-us工作得很好。

Any other thoughts?

还有其他想法吗?

回答by Gumbo

Put this rule before your rule:

将此规则放在您的规则之前:

RewriteRule !^(fr|en)/ /en%{REQUEST_URI} [L,R=301]

Or more general:

或更一般的:

RewriteRule !^[a-z]{2}/ /en%{REQUEST_URI} [L,R=301]