So I want to build a multilingual website.
At first I thought about setting language parameters through cookies, but Google says that it is not recomended. And I don't want to mess with Google because most of my visitors comes from search engines.
Google suggests subdomains likes lang.example.com or directories like example.com/lang
But I don't want to create multiple subdomains and directories in my host, so I will just use lang parameter in the URL like example.com/home?lang=de or example.com/about?lang=fr
And of course I want it to look like directories. I tried multiple htaccess configurations, but it either doesn't work at all or messes up my other rewrite rules with multiple parameters.
My old working htaccess looks like this:
RewriteRule ^about/?$ ?page=about [NC,QSA]
RewriteRule ^home/?$ ?page=home [NC,QSA]
RewriteRule ^browse/?$ ?page=browse [NC,QSA]
RewriteRule ^contact/?$ ?page=about&what=contact [NC,QSA]
RewriteRule ^message/?$ ?page=about&what=send [NC,QSA]
RewriteRule ^browse/([^/]+)$ ?page=make&make=$1 [NC,L,QSA]
RewriteRule ^browse/(.+)/(.+)/?$ ?page=browser&make=$1&ser=$2 [NC,L,QSA]
I tried adding
RewriteRule ^de/(.*)$ /$1?lang=de_DE [L,QSA]
and similar configurations, but it never works how I want.
Any ideas how to make it work?
Have your rules like this:
RewriteEngine On
# handle URIs with language parameters like /en/, /it/, /de/, /fi/ etc
RewriteRule ^([a-z]{2})/(about|home|browse)/?$ ?page=$2&lang=$1 [NC,QSA,L]
RewriteRule ^([a-z]{2})/contact/?$ ?page=about&what=contact&lang=$1 [L,NC,QSA]
RewriteRule ^([a-z]{2})/message/?$ ?page=about&what=send&lang=$1 [L,NC,QSA]
RewriteRule ^([a-z]{2})/browse/([^/]+)/?$ ?page=make&make=$2&lang=$1 [NC,L,QSA]
RewriteRule ^([a-z]{2})/browse/([^/]+)/([^/]+)/?$ ?page=browser&make=$2&ser=$3&lang=$1 [NC,L,QSA]
# defaulting to english
RewriteRule ^(about|home|browse)/?$ ?page=$1&lang=en [NC,QSA,L]
RewriteRule ^contact/?$ ?page=about&what=contact&lang=en [L,NC,QSA]
RewriteRule ^message/?$ ?page=about&what=send&lang=en [L,NC,QSA]
RewriteRule ^browse/([^/]+)/?$ ?page=make&make=$1&lang=en [NC,L,QSA]
RewriteRule ^browse/([^/]+)/([^/]+)/?$ ?page=browser&make=$1&ser=$2&lang=en [NC,L,QSA]
Related
im quite new to rewrite rules.
I can manage with one variable and thats it.
I have webpage Where the rewriterule is:
RewriteCond %{HTTP_HOST} ^www\.someserver\.com$
RewriteRule ^/?$ "http\:\/\/someserver\.com\/" [R=301,L]
RewriteRule ^(\d+)*$ ./index.php?comp=$1
RewriteRule ^(\d+)*/$ ./index.php?comp=$1
And it all work fine as it should. But now as i want 1 more variable into URL
i cant get it to work.
Right now the $1 is only numbers.
someserver.com/1554886
But i want two variable.
someserver.com/1554886-SOMENAME-WHATEVER-WORD-AND-HOW-MANY
But it wont show.
i tried smth like this:
RewriteRule ^([^-]*)-([^-]*)$ ./index.php?comp=$1&string=$2 [L]
How do i get it to work?
Do i have to make some changes in the php side as well?
everything what comes after the number part of the URL is there only for
SEO, the number is Unique
You need one more rule to handle two parameters:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(someserver\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(\d+)/?$ index.php?comp=$1 [L,QSA]
RewriteRule ^(\d+)-(.+)/?$ index.php?comp=$1&string=$2 [L,QSA]
I want to make my new website project multilingual. My idea is to redirect files to an imaginary language folder(like www.domain.com/en/sub/content.php and so on).
RewriteRule ^en/(.*)$ /$1?lang=en [L]
RewriteRule ^de/(.*)$ /$1?lang=de [L]
RewriteRule ^es/(.*)$ /$1?lang=es [L]
RewriteRule ^ru/(.*)$ /$1?lang=ru [L]
It works well for physically existing test files, even down a few folders...
But it doesn't work combined with another rewrite.
To get "www.domain.com/liga.php?whatleague=firstleague&matchday=7" to look like "www.domain.com/firstleague/2013/7/"
I used:
RewriteRule ^firstleague/2013/([0-9]+)/?$ /liga.php?whatleague=firstleague&matchday=$1 [L]
The redirect alone works fine, BUT the "lang" parameter doesn't get passed on when I add the language folder (www.domain.com/de/firstleague/2013/7/).I tried it in different order in the htaccess file, but with the same result.
PS: english is not my primary language.
You don't need another rule, just add QSA flag in you firstleague folder:
RewriteRule ^en/(.*)$ /$1?lang=en [L,QSA]
RewriteRule ^de/(.*)$ /$1?lang=de [L,QSA]
RewriteRule ^es/(.*)$ /$1?lang=es [L,QSA]
RewriteRule ^ru/(.*)$ /$1?lang=ru [L,QSA]
RewriteRule ^(firstleague)/2013/([0-9]+)/?$ /liga.php?whatleague=$1&matchday=$2 [L,QSA]
Situation
I'm using Zend framework, and thus attempt to call the folder by address ends in a fiasco and we gets the error: 'Invalid controller specified'. I needed to hook up additional forum to application in a separate folder. I change .htaccess file as follow:
RewriteRule ^forum(.*)$ forum$1 [L]
and for a while it was good, until I realized the fact that the page exists in two versions in two domains (php recognizes domain [.pl/.co.uk] and selects the language). So I decided to separate forums as a two separate modules (for example, the folder for Polish version forum will be: forum_pl, and for UK it will be: forum_uk - two different forums)
What's the problem?
It needs to rewrite .htaccess file to work like this:
IF: www.domena.pl/forum
THEN: open to the script from the folder /forum_pl
IF: www.domain.co.uk/forum
THEN: open to the script from the folder /forum_uk.
The worst thing is that when I tried to fix .htaccess file adding RewriteCond then the first redirect no longer work correctly, even after returning to the original (shown above) version.
Please help me and sorry if I did not find answers already given earlier.
EDIT:
First working solution
RewriteRule ^forum_pl(.*)$ forum_pl$1 [L]
RewriteRule ^forum_uk(.*)$ forum_uk$1 [L]
RewriteCond %{HTTP_HOST} ^www.domena.pl(.*) [NC]
RewriteRule ^forum(.*)$ forum_pl$1 [L]
RewriteCond %{HTTP_HOST} ^www.domain.co.uk(.*) [NC]
RewriteRule ^forum(.*)$ forum_uk$1 [L]
It's look like it wants to work but, problems that needs solution is (TODO):
When you type: www.domain.pl/forum it gets you to www.domain.pl/forum_pl/ but if you type: www.domain.pl/forum/ it gets you to www.domain.pl/forum/ (read from forum_pl). How to make that it works in first situation same, as in second.
EDIT:
My last solution:
#FORUM
#PL
RewriteCond %{REQUEST_URI} ^/forum$ [NC]
RewriteRule ^(.*)$ forum/ [R=301,L]
RewriteRule ^forum_pl(.*)$ forum_pl$1 [L]
RewriteCond %{REQUEST_URI} ^/forum_pl(.*)$ [NC]
RewriteRule ^(.*)$ http://www.domena.pl/forum/ [R=301,L] #Dosen't work properly (?)
RewriteCond %{HTTP_HOST} ^www.domena.pl(.*) [NC]
RewriteCond %{REQUEST_URI} ^/forum(/?)$ [NC]
RewriteRule ^forum[^/](.*)$ forum_pl/$1 #Dosen't work properly, but without it crash!
RewriteRule ^forum(.*)$ forum_pl$1 [L]
#UK
RewriteCond %{REQUEST_URI} ^/forum_uk(.*)$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/forum/ [R=301,L] #Dosen't work properly (?)
RewriteCond %{HTTP_HOST} ^www.domain.co.uk(.*) [NC]
RewriteCond %{REQUEST_URI} ^/forum(/?)$ [NC]
RewriteRule ^forum[^/](.*)$ forum_uk/$1 #Dosen't work properly, but without it crash!
RewriteRule ^forum(.*)$ forum_uk$1 [L]
Final, working version:
RewriteRule ^forum_pl(.*)$ forum_pl$1 [L]
RewriteRule ^forum_uk(.*)$ forum_uk$1 [L]
RewriteRule ^forum_pl(.*)$ http://www.domena.pl/forum$1 [R=301,L] # Not working!
RewriteRule ^forum_uk(.*)$ http://www.domain.co.uk/forum$1 [R=301,L] # Not working!
# Normalize URL first:
RewriteRule ^forum$ forum/ [R=301,L]
# redirect to polish version of web under forum_pl if on .pl TLD and
# request is made to /forum/ (already normalized)
RewriteCond %{HTTP_HOST} =www.domena.pl [NC]
RewriteRule ^forum(.*)$ forum_pl$1 [L]
# redirect to english version of web under forum_en in on .uk TLD and
# request is made to /forum/
RewriteCond %{HTTP_HOST} =www.domain.co.uk [NC]
RewriteRule ^forum(.*)$ forum_uk$1 [L]
It is a pity that you can call from your browser forum_uk and forum_pl folders manually.
Not sure if I oriented correctly in your long question with some evolution :-)
Lets try this:
# Normalize URL first:
RewriteRule ^forum$ forum/ [R=301,L]
# redirect to polish version of web under forum_pl if on .pl TLD and
# request is made to /forum/ (already normalized)
RewriteCond %{HTTP_HOST} =www.domena.pl [NC]
RewriteRule ^forum/(.*)$ http://www.domena.pl/forum_pl/$1 [R=301,L]
# redirect to english version of web under forum_en in on .uk TLD and
# request is made to /forum/
RewriteCond %{HTTP_HOST} =www.domain.co.uk [NC]
RewriteRule ^forum/(.*)$ http://www.domain.co.uk/forum_uk/$1 [R=301,L]
If you need the rewites to act differently, let me know
I have a site (sports.com, let's say) and I'd like to set up mod_rewrite so that a visitor to "football.sports.com/america" is shown content from "sports.com/stuff/place.php?id=america". I'd still like visitors to the naked "sports.com" to see "stuff/index.php".
Sorry, I'm a real newb with this. I've tried and failed several times, heres what I have so far...
IndexIgnore *
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} football\.sports\.com [NC]
RewriteRule ^([^/.]+)$ stuff/place.php?id=$1 [L]
RewriteRule (.*) stuff/index.php?%{QUERY_STRING}
Any help would be massively appreciated.
You can do this like:
RewriteRule ^america$ america/ [NC]
RewriteRule ^america/$ stuff/place.php?id=america [NC,L]
Redirects football.sports.com/america[/] to football.sports.com/stuff/place.php?id=america. This is a static rule: it won't work with france for example.
If you need a generic way you need :
RewriteRule ^([^/.]+)$ $1/ [NC]
RewriteRule ^(.*)/$ stuff/place.php?id=$1 [NC,L]
To get naked site to be redirected, add:
RewriteRule ^$ stuff/index.php [L]
Redirects football.sports.com/ to football.sports.com/stuff/index.php
Eventually you can add the query string of necessary.
I want to have my site urls look like
http://example.com/place/info?var=info&morevars=ifneeded
Place and info are also variables but they have a fixed name, the ones after would vary. EDIT This is the url I am trying to rewrite
http://example.com/test.php?place=test&action=info&var=info&morevars=ifneeded
This is what I have so far
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ test.php?place=$1 [NC]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ test.php?place=$1&action=$2 [NC]
I think there a way to do this with {QUERY_STRING} but I can't get it to work just 500 errors or it don't make a differences.
You have set the QSA flag that automatically appends the original requested query to the new one:
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ test.php?place=$1 [NC,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ test.php?place=$1&action=$2 [NC,QSA]
You're missing the first /
RewriteEngine on
RewriteRule ^/([A-Za-z0-9-]+)/ test2.php?place=$1 [NC]
RewriteRule ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/ test2.php?place=$1&action=$2 [NC]