Website Language - htaccess RewriteRule - .htaccess

I'm trying to use rules and conditions with .htaccess but don't know how to do what I need.
Here's the scene:
I have a website with many languages. (en|es|pt)
I want to use the urls like this:
www.site.com/home/
(original url): www.site.com/index.php?p=home
(opens home.php - session language)
And like this:
www.site.com/en/home/
(original url): www.site.com/index.php?l=en&p=home
(opens home.php - en language)
What are the eules/conditions that I need to create to detect the language in url?
TIA
SOLUTION:
RewriteRule ^(en|es|pt)?\/?([a-z0-9A-Z,_-]+)\/?$ index.php?l=$1&p=$2 [QSA,L]
Thanks
Note:
"current solution does also allow something like /enfoobar to be rewritten to /index.php?l=en&p=foobar as the / is independent of the language indicator" - Gumbo

You might probably use something like this
RewriteEngine on
RewriteBase /
RewriteRule ^(en|es|pt)/(.*) /index.php?l=$1&p=$2 [QSA,L]

For any 2digits characters languages
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^[a-zA-Z][a-zA-Z]/index.html$ index.html?language=$1
RewriteRule ^index.html$ /index.php [L]

Related

Urls rewriting for a multilingual webpages

My actual page structure is like this:
https://example.com/try
What would be the best .htaccess rules to convert it to:
https://example.com/fr/essayer or https://example.com/en/try
What I have tested:
RewriteRule ^(fr|en)/(.*)\.php$ $2.php?lang=$1 [L,QSA]
But I can't change the page name to the french one.
Thanks.
If you want to use URLs like https://example.com/fr/pageA or https://example.com/en/pageA while under the hood still serving up https://example.com/fr/pageA or https://example.com/pageA you could use a rule like this:
RewriteRule ^(en|fr|de)/(.+) $2 [QSA,L]
Demo here: http://htaccess.mwl.be?share=c26eb9b8-7d31-51df-9072-0f218032dba3
Note that this does not pass the language code over, you could achieve that like so if needed:
RewriteRule ^(en|fr|de)/(.+) $2?lang=$1 [QSA,L]

mask URL (with parameters) with htaccess

i need to mask a long, stupid php URL with a relatively simple URL with htaccess.
For example, i need to mask -
http://mysite.com/index.php?this=that&this=that&whatevs=this&that=&id=5
with this simple URL -
http://mysite.com/mypage.php
I know php can do the stuff very easily by fetching the content of that page and displaying it. But in my conditions, there are some problems with ajax calls and some other things.
Moreover, i prefer to use htaccess. I did some research but htaccess codes are very confusing to me.
I took the liberty of removing the .php part from mypage.php as it has no real use, and makes the url look less pretty.
RewriteEngine On
RewriteBase /
RewriteRule ^mypage$ /index.php?this=that&this=that&whatevs=this&that=&id=5 [L,QSA]
So you only want redirect http://mysite.com/index.php?this=that&this=that&whatevs=this&that=&id=5 to http://mysite.com/mypage.php?
Then use this RewriteRule:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} this=that&this=that&whatevs=this&that=&id=5
RewriteRule ^index.php$ http://mysite.com/mypage.php? [L,R=301]

Rewrite basic $_GET variable to friendly URL using mod_rewrite

I know questions similar to this are common. I just don't even know where to begin with rewrite rules with the /'s and .'s I don't know how to retrofit other peoples solutions to mine. Onto my situation:
I am using a basic get on the index.php file that looks like
http://www.example.com/index.php?page=about
I want to rewrite that to
http://www.example.com/about
I know this is fairly simple rewrite wise, but its just a totally different language which I have tried and failed to comprehend. Thanks
Try this:
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /index.php?page=$1 [L]
Or even:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(?!index.php\b).* index.php?page=$0 [L,QSA]
Note:
You should always set the RewriteBase in an .htaccess file.
I've generalised this so that index.php picks up any string which isn't mapping to a real file
The (?!index.php\b) is a regexp which says "but don't match to index.php"
You will need the [QSA] flag if your requests can contain request parameters. This merges them.

Add language directory using .htaccess

Hi Stackoverflow community,
I've been reading through most of the other .htaccess questions, and tried different solutions on my .htaccess file but with no success.
Here's the thing: I have a website (based on CodeIgniter) that already has a good SEO positioning. Now, the customer wants it translated to another language, but I dont want to lose that SEO so I thought of redirecting a URL like http://www.domain.com/contactUs to http://www.domain.com/en/contactUs, being (en) the current positioned language. CI would handle the other language as usual without problem. Im currently doing tests on localhost, so the following rules will have only "domain" instead of "domain.com".
I've tried these Rewrite rules:
RewriteEngine on
Options +FollowSymlinks
RewriteCond ^(.*)/domain/(.*) !^/en/
RewriteRule ^(.*)/domain/(.*)$ $1/domain/en/$2
I want the RewriteCond to filter URLs coming in the new language so that the RewriteRule is not executed. What am I doing wrong?
Do this:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/en/ [NC]
RewriteRule ^(.*)$ /en/$1 [L]
You should put all your text in unique language files, then load them like so:
$this->config->set_item('language',"your_language");
$this->lang->load('default', "your_language");
To write out the content, the use:
$this->lang->line('message_id');
More info:
http://codeigniter.com/user_guide/libraries/language.html
This should more efficient than creating a duplicate site while still being SEO friendly. In fact, it should be more SEO friendly.
RewriteCond $2 !^en/
RewriteRule ^(.*)/domain/(.*)$ $1/domain/en/$2

PHP page access without extension and variable

How can I create a PHP page system that we access without .php extension and without ?page= variable?
There's a simple exemple:
http://www.exemple.com/portfolio/1
Instead of:
http://www.exemple.com/portfolio.php?id=1
It would be great for me because I'm a web designer and this is a thing I never did. So it would be good to know this! Also I will use this in my website.
you would need the following in your .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*[^/])/?$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule .+ %1.php [QSA,L]
You're looking for mod_rewrite.
RewriteEngine On
RewriteRule ^/profile$ /account.php?profile [L]
Create .htaccess file in your web root and enter following.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^account/([0-9]+)$ account.php?id=$1
OR
See mod_rewrite, in case URL rewriting, if you trying to do such.
You can use multiviews, or better and more used: mod_rewrite module from apache. It can be via a .htaccess file where you create rewrite rules like
RewriteEngine On
RewriteRule ^(.*)?(.*)$ /$1.php?Action=$2 [L]

Resources