.htaccess 带有语言的漂亮 URL

发布于 2025-01-13 23:08:27 字数 1245 浏览 2 评论 0原文

我有一个 php 网站,加载页面如下。

    if ( $secretfile && file_exists( $moddir . $name . "/" . $secretfile . ".php" ) ) {
  include_once( $moddir . $name . "/" . $secretfile . ".php" );
} else {
  if ( isset( $_GET[ 'mod' ] ) ? $_GET[ 'mod' ] : '' ) {
    $secretmodule = isset( $_GET[ 'mod' ] ) ? $_GET[ 'mod' ] : '';
    if ( $secretmodule && file_exists( $moddir . $name . "/page.php" ) ) {
      include_once( $moddir . $name . "/page.php" );
    } else {
      include_once( $moddir . "404.php" );
    }
  } else {
    include_once( $moddir . "homepage.php" );
  }
}

我正在使用 htaccess 来美化网址,如下所示。

RewriteRule ^([^/]*)/$ /index.php?mod=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?mod=$1&file=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ /index.php?mod=$1&file=$2&proid=$3 [L]

我想在会话中添加语言,但我不想更改主要语言的 url(已索引)。只有其他语言才会添加额外的变量。

eg: 
main language(de) : www.site.com/module/file/     /index.php?mod=$1&file=$2 [L]
other lang (en) : www.site.com/en/module/file/    /index.php?lang=$1mod=$2&file=$3 [L]
other lang (fr) : www.site.com/fr/module/file/    /index.php?lang=$1mod=$2&file=$3 [L]

我只是不确定如何为这些配置设置 htaccess。

I have a php website, loading the pages as below.

    if ( $secretfile && file_exists( $moddir . $name . "/" . $secretfile . ".php" ) ) {
  include_once( $moddir . $name . "/" . $secretfile . ".php" );
} else {
  if ( isset( $_GET[ 'mod' ] ) ? $_GET[ 'mod' ] : '' ) {
    $secretmodule = isset( $_GET[ 'mod' ] ) ? $_GET[ 'mod' ] : '';
    if ( $secretmodule && file_exists( $moddir . $name . "/page.php" ) ) {
      include_once( $moddir . $name . "/page.php" );
    } else {
      include_once( $moddir . "404.php" );
    }
  } else {
    include_once( $moddir . "homepage.php" );
  }
}

I am using htaccess to beautify the url as below.

RewriteRule ^([^/]*)/$ /index.php?mod=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?mod=$1&file=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ /index.php?mod=$1&file=$2&proid=$3 [L]

I want to add languages in session but i dont want to change the url of the main language (already indexed). only the other languages will be added an extra variable.

eg: 
main language(de) : www.site.com/module/file/     /index.php?mod=$1&file=$2 [L]
other lang (en) : www.site.com/en/module/file/    /index.php?lang=$1mod=$2&file=$3 [L]
other lang (fr) : www.site.com/fr/module/file/    /index.php?lang=$1mod=$2&file=$3 [L]

I am just not sure how to setup the htaccess for these configurations.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九厘米的零° 2025-01-20 23:08:27

您可以在 .htaccess 中拥有这些规则集:

RewriteEngine On

# ignore all files are directories from rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# rules with any lang component
RewriteRule ^(en|fr)/([^/]*)/$ index.php?lang=$1&mod=$2 [L,QSA]
RewriteRule ^(en|fr)/([^/]*)/([^/]*)/$ index.php?lang=$1&mod=$2&file=$3 [L,QSA]
RewriteRule ^(en|fr)/([^/]*)/([^/]*)/([^/]*)/$ index.php?lang=$1&mod=$2&file=$3&proid=$4 [L,QSA]

# rules without any lang component
RewriteRule ^([^/]*)/$ index.php?lang=de&mod=$1 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/$ index.php?lang=de&mod=$1&file=$2 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ index.php?lang=de&mod=$1&file=$2&proid=$3 [L,QSA]

You can have these set of rules in .htaccess:

RewriteEngine On

# ignore all files are directories from rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# rules with any lang component
RewriteRule ^(en|fr)/([^/]*)/$ index.php?lang=$1&mod=$2 [L,QSA]
RewriteRule ^(en|fr)/([^/]*)/([^/]*)/$ index.php?lang=$1&mod=$2&file=$3 [L,QSA]
RewriteRule ^(en|fr)/([^/]*)/([^/]*)/([^/]*)/$ index.php?lang=$1&mod=$2&file=$3&proid=$4 [L,QSA]

# rules without any lang component
RewriteRule ^([^/]*)/$ index.php?lang=de&mod=$1 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/$ index.php?lang=de&mod=$1&file=$2 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ index.php?lang=de&mod=$1&file=$2&proid=$3 [L,QSA]
煞人兵器 2025-01-20 23:08:27

根据您显示的尝试,请尝试以下操作。确保您的 index.php 文件和 htaccess 这两个文件都存在于您的情况下的同一文件夹(根目录)中。

在测试您的网址之前,请确保清除浏览器缓存。

RewriteEngine ON
##Rules for en OR fr languages URLs to be rewritten in backend.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(en|fr)/([^/]*)/([^/]*)/?$ /index.php?lang=$1mod=$2&file=$3 [QSA,NC,L]

##Rules for links which are not started with en OR fr links.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ /index.php?lang=$1mod=$2&file=$3 [QSA,NC,L]

With your shown attempts, please try following. Make sure your index.php file and htaccess both files are present in same folder(root) in your case.

Please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
##Rules for en OR fr languages URLs to be rewritten in backend.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(en|fr)/([^/]*)/([^/]*)/?$ /index.php?lang=$1mod=$2&file=$3 [QSA,NC,L]

##Rules for links which are not started with en OR fr links.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ /index.php?lang=$1mod=$2&file=$3 [QSA,NC,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文