Trying to orientate myself through the .htaccess jungle, I've now narrated down my rewrite problems to one - adding a language suffix upon entering the page / typing in an address that doesn't have such one.
For example: a visitor types mysite.com/kontakt
. This should take them to the default language of the site, i.e. mysite.com/sv/kontakt
. Or, just typing in mysite.com
should take him/her to mysite.com/sv/
.
I'm developing this site locally using MAMP, and the site is located in a subdirectory, and here's the tricky part... How do I sort this out in the .htaccess file?
Current code used:
RewriteBase /mysite开发者_JS百科/
RewriteCond %{REQUEST_URI} !^(sv|en)
RewriteRule ^(.*)$ sv/$1 [L,R=301]
And that takes me from localhost/mysite/
to localhost/sv/
. Not exactly right.
Try this one:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /mysite/
RewriteRule ^(?!(?:sv|en)/)(.*)$ sv/$1 [R=301,L]
This needs to be placed into .htaccess file into /mysite/ folder.
If accessing
localhost/mysite/sv
without trailing slash, the rule will redirect it tolocalhost/mysite/sv/sv
as it expects that the language part (sv
oren
) will be followed by the slash/
.
P.S.
And look into setting up a VirtualHost -- then there will be much less (if none at all) of such "tricky parts" with accessing your website via http://localhost/mysite/sv/kontakt
(I'm sure you will agree, that http://mysite.dev/sv/kontakt
sounds and looks better).
精彩评论