开发者

.htaccess - add language sub directory by default "sv/" (redirect)

开发者 https://www.devze.com 2023-03-21 15:52 出处:网络
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

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]
  1. This needs to be placed into .htaccess file into /mysite/ folder.

  2. If accessing localhost/mysite/sv without trailing slash, the rule will redirect it to localhost/mysite/sv/sv as it expects that the language part (sv or en) 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).

0

精彩评论

暂无评论...
验证码 换一张
取 消