RewriteRule for htaccess隐藏默认语言

时间:2011-11-15 07:51:57

标签: apache .htaccess mod-rewrite lighttpd multilingual

在Zend配置默认值中,我有以下规则:

SetEnv APPLICATION_ENV offline

RewriteEngine On
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

当我尝试添加fallowing line时,请注意:

RewriteRule ^en/(.*) $1 [L]

根据结果,我会将per http://example.com/en/something重写为http://example.com/something 现在,我有两个链接分开工作。

编辑:

SetEnv APPLICATION_ENV offline

RewriteEngine On
Options +FollowSymlinks

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteCond %{REQUEST_URI} !^/en/
RewriteRule ^.*$ index.php [NC,L]

RewriteRule ^en/(.*) /$1 [L,R]

这会将默认语言网址直接重定向到网站根目录! 非常感谢。

有LigHTTPD:

$HTTP["url"] != "^/en/" {
    url.rewrite-once = (
        "^/.*" => "index.php?/$1",
    )
}
url.redirect = (
    "^/en/.*" => "/$1",
)

1 个答案:

答案 0 :(得分:1)

这是因为RewriteRule ^.*$ index.php [NC,L]正在重写它,它永远不会达到您添加的规则。您需要添加一个条件,以便以/en/开头的请求不会被重写为index.php:

# you regular stuff
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

# Add this condition
RewriteCond %{REQUEST_URI} !^/en/
RewriteRule ^.*$ index.php [NC,L]

# Now you can rewrite /en
RewriteRule ^en/(.*) $1 [L,R]

已修改:example.com/en/something被重定向到example.com/something,然后重写为index.php