2012-04-08 84 views
2

我有什麼:的.htaccess重定向到語言子文件夾

  • 有兩種語言安裝:德語和英語
  • 語言版本與子文件夾管理(www.domain.com/de/和www.domain.com/en/)
  • 機:TYPO3的4.5.4/RealUrl安裝
  • 的mod_rewrite啓用的作品

我想要做的:沒有語言的子文件夾

  • 將請求重定向到德語版
  • 例如domain.com/any/test/folder/到domain.com/ /任何/測試/文件夾

我的嘗試:

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

發生的歷史問題:

  • 所有請求被轉發到domain.com/de/index.php
  • 它導致d錯誤310(net :: ERR_TOO_MANY_REDIRECTS)
  • 也許它與Typo3的htaccess條目失敗?

我的洞的.htaccess文件:

# 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] 

那麼,我該怎麼辦?

+0

這肯定是關係到正則表達式,但我不是在家裏與重寫規則給直接的建議。我嘗試http://martinmelin.se/rewrite-rule-tester/,但它不會識別您的「RedirectMatch永久性」規則。至於ERR_TOO_MANY_REDIRECTS,是否可以再次提供(並再次)提供生成的URL進行轉換? – buckley 2012-04-08 18:11:34

+0

你也可以使用內容協商'Accept-Language:de'頭來幫助確定初始重定向http://httpd.apache.org/docs/2.2/content-negotiation.html甚至是geoip/geodns – 2013-12-12 12:43:01

回答

4

這個什麼:

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

+ RewriteCond% {REQUEST_FILENAME}!-f這是我的選擇,謝謝! – kernfrucht 2012-05-04 14:59:54

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] 
+1

我嘗試了兩種解決方案(湯姆和Qtax的),但他們只工作沒有否定!所以像RewriteCond%{REQUEST_URI}^/(en | de)/完美的工作!通過添加否定,「重定向過多」 - 重定向到http://domain.com/hallo/hallo/hallo/hallo/hallo/hallo/hallo/hallo/hallo/hallo/hallo/hallo/hallo/hallo後發生錯誤/ hallo/hallo/hallo/hallo/hallo/hallo/hallo/path/i/entered/Code:'RewriteCond%{REQUEST_URI}!^ /(en | de) RewriteRule ^(。*)$/hallo/RewriteRule ^(。*)$/de/$ 1 [L,R = 301]' – kernfrucht 2012-04-09 15:00:49

+0

因爲你不能去管理面板: RewriteCond%{REQUEST_URI}!^ /(en | de | typo3)/ RewriteRule^301] – 2016-11-09 14:33:04

0

你其實並不需要使用htacces你可以配置這個在realUrl配置中的行爲。只要在你的配置數組定義此部分:

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

這會給你一個重定向到../德/ ...

+0

這是真的,但是這種重定向與我認爲的htaccess相比非常低 – kernfrucht 2012-05-04 15:00:19