Mod-rewrite多语言

时间:2015-05-18 13:27:54

标签: .htaccess mod-rewrite

服务器上有一些不同语言的html文件:

company.html
index.html
en_company.html
en_index.html

我想像这样重定向:

www.site.com/ > www.site.com/index.html
www.site.com/company/ > www.site.com/company.html

www.site.com/en/ > www.site.com/en_index.html
www.site.com/en/company/ > www.site.com/en_company.html

为什么不起作用?

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/en/]*)/$ en_$1.php [L,NC,NE,QSA]

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以将此代码放入htaccess(必须位于根文件夹中)

Options -MultiViews

RewriteEngine On
RewriteBase /

# don't touch existing files/folders
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# rewrite /lang/ to /lang_index.html
RewriteRule ^([a-z]{2})/$ $1_index.html [L]

# rewrite /lang/page/ to /lang_page.html
RewriteCond %{DOCUMENT_ROOT}/$1_$2\.html -f
RewriteRule ^([a-z]{2})/([^/]+)/$ $1_$2.html [L]

# rewrite /page/ to /page.html
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^([^/]+)/$ $1.html [L]