使用htaccess URLrewrite设置语言

编程入门 行业动态 更新时间:2024-10-19 22:21:09
本文介绍了使用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/en/ 一个
  • mydomain / en / 两个
  • mydomain/de/ 一个
  • mydomain / de / 两个
  • mydomain/en/one
  • mydomain/en/two
  • mydomain/de/one
  • mydomain/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/en/ 一个 / id45 如果我想将id45作为变量传递给 one.php ,我必须在htaccess中添加哪一行?

ONE MORE ADDITIONAL QUESTION For mydomain/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设置语言

本文发布于:2023-11-01 18:11:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550045.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语言   htaccess   URLrewrite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!