【问题标题】:htaccess 301 redirect to language subdirectoryhtaccess 301 重定向到语言子目录
【发布时间】:2014-11-08 14:50:32
【问题描述】:

我将网站网址更改为接受不同的语言,因此,我将网站内容移动为默认语言

从:www.site.com 到:www.site.com/en

现在,我从 php 进行重定向,如果 $_GET['lang'] 不存在或 $_GET['path'] 存在,重定向到 site.com/en 或 site.com/en/(path) 但我认为 301 重定向从根到文件夹更好。

我该如何编写这条规则?

这是我的 htaccess 文件:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteRule ^download/(.*)$ php/download.php?id=$1 [L]

# with language
RewriteRule ^([a-z]{2})/p/(.*)$ single.php?lang=$1&hash=$2 [L]
RewriteRule ^([a-z]{2})$ index.php?lang=$1 [L,NC,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ index.php?path=$1 [NC,L,QSA]

谢谢

【问题讨论】:

  • 需要澄清一下。什么是 URL 示例以及应如何处理这些 URL。

标签: php apache .htaccess mod-rewrite redirect


【解决方案1】:

如果我的理解是正确的,这会做你想要的。

Updated .htaccess(与 cmets 一起更好地理解)

RewriteEngine on
RewriteBase /

# If the hostname is a `www` subdomain, redirect to the parent domain
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# Internal rewrite of Download request URL to download PHP script
RewriteRule ^download/(.*)$ php/download.php?id=$1 [L]

# If request path is just root `/` (redirect to default language site)
RewriteRule ^$  /en [R=301,L]

# If request path is a language code,
# internally rewrite to index.php with `lang` query paramater
RewriteRule ^([a-z]{2})$ index.php?lang=$1 [L,NC,QSA]

# If request path begins with lang code and has a hash segment,
# internally rewrite to single.php with `lang` and `hash` query parameters
RewriteRule ^([a-z]{2})/p/(.*)$ single.php?lang=$1&hash=$2 [L]

# If request path begins with lang code and has additional segements
RewriteRule ^([a-z]{2})/(.*)$ index.php?lang=$1&path=$2 [NC,L,QSA]

# For every other request path not having a language code,
# if path is not an existing file or directory,
# redirect to a path prefixing default lang code before the requested path
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$  /en/$1 [NC,L,R=301]

【讨论】:

    猜你喜欢
    • 2021-04-04
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    相关资源
    最近更新 更多