的.htaccess重写默认的语言文件夹? [英] .htaccess rewrite to default language folder?

查看:208
本文介绍了的.htaccess重写默认的语言文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我想有一个重写规则自动重写到连接文件夹中,如果有人试图进入 mydomain.com/about-us .PHP

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

仅供参考,我也已经到位,消除扩展名的重写规则,所以我真的想确保 mydomain.com/about-us 改写为 mydomain.com/en/about-us 。这里是我做这个现有的重写规则:

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-us rewrites to mydomain.com/en/about-us. Here's my existing rewrite rule that does this:

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

这有意义吗?谁能帮助我在这里?

That make sense? Anyone help me out here?

编辑:

@Gumbo -

@Gumbo -

下面就是我的的.htaccess 文件看起来像(这是所有在它):

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

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 ^(.*)$ $1.php

这个文件是在我的网站(在语言文件夹没有)的根。我重新装我的web服务器,只是为了安全起见,当我尝试去 mydomain.com/about-us 我还收到此错误信息:

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:

Not Found

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

...其中 mydomain.com/en/about-us 作品就好。

任何其他想法?

推荐答案

将这个规则:

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

或者更一般的:

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

这篇关于的.htaccess重写默认的语言文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆