.htaccess重定向到语言子文件夹

时间:2012-04-08 17:00:19

标签: regex .htaccess mod-rewrite typo3 realurl

我有什么:

  • 安装了两种语言:德语和英语
  • 语言版本使用子文件夹管理(www.domain.com/de/和www.domain.com/en /)
  • 机器:Typo3 4.5.4 / RealUrl已安装
  • mod_rewrite启用了作品

我想做什么:

  • 将没有语言子文件夹的请求重定向到德语版本
  • e.g。 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]

那么,我该怎么办?

3 个答案:

答案 0 :(得分:4)

这个怎么样:

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

答案 1 :(得分: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]

答案 2 :(得分:0)

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

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

这将为您提供重定向到../ de /...