【问题标题】:.htaccess redirect to language subfolder.htaccess 重定向到语言子文件夹
【发布时间】:2012-04-21 07:38:03
【问题描述】:

我有什么:

  • 安装了两种语言:德语和英语
  • 语言版本通过子文件夹(www.domain.com/de/ 和 www.domain.com/en/)进行管理
  • 机器:已安装 Typo3 4.5.4 / RealUrl
  • mod_rewrite 已启用且有效

我想做的事:

  • 将没有语言子文件夹的请求重定向到德语版本
  • 例如domain.com/any/test/folder/ 到 domain.com/de/any/test/folder

我尝试了什么:

RedirectMatch permanent ^/(?!(?:(?:de|en)/))(.*)$ /de/$1

出现的问题:

  • 所有请求都被定向到 domain.com/de/index.php
  • 导致错误 310 (net::ERR_TOO_MANY_REDIRECTS)
  • 可能因为 Typo3 的 htaccess 条目而失败?

我的洞 .htaccess-file:

# Enable URL rewriting
RewriteEngine On

# Change this path, if your TYPO3 installation is located in a subdirectory of the website root.
# RewriteBase /

# Rule for versioned static files, configured through:
# - $TYPO3_CONF_VARS['BE']['versionNumberInFilename']
# - $TYPO3_CONF_VARS['FE']['versionNumberInFilename']
# IMPORTANT: This rule has to be the very first RewriteCond in order to work!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ $1.$3 [L]

# Stop rewrite processing, if we are in the typo3/ directory.
RewriteRule ^(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]

# Redirect http://example.com/typo3 to http://example.com/typo3/index_re.php and stop the rewrite processing.
RewriteRule ^typo3$ typo3/index_re.php [L]

#301 redirection for language mode
RedirectMatch permanent ^/(?!(?:(?:de|en)/))(.*)$ /de/$1

# If the file/symlink/directory does not exist => Redirect to index.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

# Main URL rewriting.
RewriteRule .* index.php [L]

那么,我该怎么办?

【问题讨论】:

  • 这肯定与正则表达式有关,但我不喜欢重写规则来提供直接建议。我尝试了martinmelin.se/rewrite-rule-tester,但它无法识别您的“RedirectMatch permanen”规则。至于 ERR_TOO_MANY_REDIRECTS,是否会再次(又一次)提供生成的 url 以进行转换?
  • 您也可以使用内容协商 Accept-Language: de 标头来帮助确定初始重定向 httpd.apache.org/docs/2.2/content-negotiation.html 甚至 geoip / geodns

标签: regex .htaccess mod-rewrite typo3 realurl


【解决方案1】:

这个呢:

RewriteRule ^/?(?!de/|en/)(.*) /de/$1 [L,R=301]

【讨论】:

  • + RewriteCond %{REQUEST_FILENAME} !-f 这是我的选择,谢谢!
【解决方案2】:

你为什么不使用更简单的东西,像这样:

# if user didn't specify a valid language subfolder
RewriteCond %{REQUEST_URI} !^/(en|de)/
# redirect user to default language subfolder
RewriteRule ^(.*)$ /de/$1 [L,R=301]

【讨论】:

  • 我尝试了两种解决方案(Tom's 和 Qtax's),但它们只能在没有否定的情况下工作!所以像 RewriteCond %{REQUEST_URI} ^/(en|de)/ 这样的东西可以完美地工作!通过添加否定,重定向到domain.com/hallo/hallo/hallo/hallo/hallo/hallo/hallo/hallo/… 代码:RewriteCond %{REQUEST_URI} !^/(en|de) RewriteRule ^(.*)$ /hallo/$1 [L,R=301] 后发生“重定向过多”错误
  • 因为您无法进入管理面板:RewriteCond %{REQUEST_URI} !^/(en|de|typo3)/ RewriteRule ^(.*)$ /de/$1 [L,R=301]
【解决方案3】:

您实际上不需要使用 htacces,因为您可以在 realUrl 配置中配置此行为。只需在您的配置数组中定义这部分:

    'preVars' => array(
      array(
        'GETvar' => 'L',
        'valueMap' => array(
            'de' => '0',
            'en' => '1',
            'fr' => '2',
            ),
        'valueDefault' => 'de',
        ),
),

这会给你一个重定向到 ../de/...

【讨论】:

  • 这是真的,但我认为这种重定向与 htaccess 相比非常低
猜你喜欢
  • 2014-03-20
  • 1970-01-01
  • 2023-03-30
  • 2013-08-27
  • 2014-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多