使用htaccess URLrewrite设置语言 [英] Set language using htaccess URLrewrite

查看:80
本文介绍了使用htaccess URLrewrite设置语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我的根目录中有两个文件: one.php two.php 。我想通过这些URL到达它们,而不必实际拥有物理目录en + de。

I have for example two files in my root directory: one.php and two.php. I would like to reach them through these urls without having to actually have the physical directories en+de.


  • mydomain.com/en/ 一个

  • mydomain.com / en / 两个

  • mydomain.com/de/ 一个

  • mydomain.com / de / 两个

  • mydomain.com/en/one
  • mydomain.com/en/two
  • mydomain.com/de/one
  • mydomain.com/de/two

因此,第一个目录将被忽略,并仅通过GET变量传递php文件的语言设置,第二个目录是不带扩展名的php文件。

So the first directory would be "ignored" and just pass a GET variable with the language setting to the php-file, second directory is the php-file without extension.

到目前为止,这是我在htaccess中使用的扩展名:

This is what I'm using in my htaccess to loose the file extension so far:

RewriteEngine on  
RewriteCond %{REQUEST_FILENAME} !-d   
RewriteCond %{REQUEST_FILENAME}\.php -f   
RewriteRule ^(.*)$ $1.php

更新/解决方案

.htaccess语言检测规则

,这似乎

UPDATE/SOLUTION
This is quite useful: .htaccess rule for language detection
and this seems to pretty much do what I wanted:

RewriteRule ^(en|de|fr)/(.*)$  $2?lang=$1 [L,QSA]   
RewriteRule ^(.*)$  $1?lang=en [L,QSA]

一个其他问题

对于mydomain.com/en/ 一个 / id45

如果我想将id45作为变量传递给 one.php ,我必须在htaccess中添加哪一行?

ONE MORE ADDITIONAL QUESTION
For mydomain.com/en/one/id45
If I wanna pass id45 as a variable to one.php what line in htaccess do I have to add?

推荐答案

您可以尝试以下规则:

DirectoryIndex index.php index.html
DirectorySlash On

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$2.php -f
RewriteRule ^(en|de|fr)/([^/]+)(?:/(.*)|)$ $2.php?lang=$1&var=$3 [L,QSA,NC]

RewriteCond %{REQUEST_FILENAME} !-d   
RewriteCond %{REQUEST_FILENAME}.php -f   
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/?$ index.php?lang=$1 [L,QSA,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ $1?lang=en [L,QSA]

这篇关于使用htaccess URLrewrite设置语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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