Urls rewriting for a multilingual webpages - .htaccess

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]

Related

Using .Htaccess url redirection

I want to rewrite my url but i cant do proper way.
mysitename.net/index.php?title=Category:Public_Companies&pagefrom=8
To :
mysitename.net/Category:Public_Companies/8
How can i do that.
If you have other pages than Category, I recommend to use this:
RewriteRule ^Category:(.+)/(.+)$ /index.php?title=Category:$1&pagefrom=$2 [L]
Otherwise, use this:
RewriteRule ^(.+)/(.+)$ /index.php?title=$1&pagefrom=$2 [L]

rerwrite url in .htaccess

I want to make the old urls point to the new pages. I'm having trouble because the old URL looks like this: http://nilandsplace.com/store/camping_eng/coghlans-campfire-cooking-forks-toaster-forks-package-of-4.html.
I want to do a URL re-right in .ht access from mywebsite.com/camping_eng/some.html to mywebsite.com/camping/some.html, were the "some.html" part is identical.
Only camping_eng changes to camping.
You might want to try this:
RewriteEngine on
RewriteRule ^camping_eng/([a-zA-Z0-9-=_.]+)/?$ http://mywebsite.com/camping/$1 [L,R=301]
This example works according to your question:
from mywebsite.com/camping_eng/some.html
to mywebsite.com/camping/some.html were the "some.html" part is identical.
RewriteEngine on
RewriteBase /store
RewriteRule ^camping_eng/(.*)$ camping/$1 [QSA,L,R=301]

httacces Rewrite URLs

I wanted to rewrite some urls from
www.example.ro/women_shoes.php to www.example.ro/women-shoes/
and with pagination looks like
www.example.ro/women_shoes.php?page=1 to www.example.ro/women_shoes/pagina-2/
www.example.ro/men_shoes.php to www.example.ro/men-shoes/
with pagination like the first one
www.example.ro/sandale_barbati.php to www.example.ro/sandale-barbati/
and so on..
for the last example i tryed
RewriteRule ^sandale-barbati/pagina-([0-9]+)/ /sandale-barbati.php?page=$1 [L]
RewriteRule ^sandale-barbati/?$ /sandale_barbati.php?$
but i need to write that for every page...
is there a way to do that automaticaly for all pages ?
thanks
If you always use the same naming for all your pages, this will work:
RewriteRule ^([a-zA-Z]+)([-_]){1}([a-zA-Z]+)/pagina-([0-9]+)/$ $1_$3.php?page=$4
RewriteRule ^([a-zA-Z]+)([-_]){1}([a-zA-Z]+)/$ $1_$3.php
RewriteRule ^([a-zA-Z_]+)/pagina-([0-9]+)/$ /$1.php?page=$2
RewriteRule ^([a-zA-Z_]+)/$ /$1.php
You can surf to /women_shoes/ and it will be the /women_shoes.php page.
You can surf to /women-shoes/ and it will be the /women_shoes.php page.
You can surf to the /shoes/ and it will be the /shoes.php page.
I hope this has met all your requirements and was of sufficient help to you. For future reference, the website http://www.regular-expressions.info/ is very handy when dealing with RegEx (such as .htaccess rewrite rules).

SEO Friendly URL Command in .htaccess

I have a dynamic page that looks like the following:
www.sitedomain.com/page.php?id=30&name=about_our_company
I want to make my website links SEO friendly by having something like the following:
www.sitedomain.com/about_our_company.html
Or
www.sitedomain.com/about_our_company
My question is: what regex/code I should have in the .htaccess file?
Thanks
This ofc has a fixed id of 30.
RewriteRule ^about_our_company/?$ /page.php?name=$1&id=30 [NC,L]
since /about_our_company doesn't include the ID, it's impossible to invent the ID correctly.
the way I do it in my own CMS is to have something like this in the .htaccess:
RewriteEngine on
RewriteRule ^(.*)$ /index.php?page=$1 [QSA,L]
then in index.php, use the $_REQUEST['page'] (assuming PHP) variable to find the right page details in the database
You could improve this to be more generic:
RewriteRule ^([^/]+)/([^/]+)?$ /page.php?id=$1&name=$2 [NC,L]
The following URL's would all match:
http://example.com/30/about_our_company
http://example.com/29/contact
http://example.com/10/our_work

HTACCESS Rewrite rules

I need help to redirect the following URL (in htaccess)
http:www.domain.com/article/the-bp-oil-spill-one-year-later/19918396/20110420/
To
http:www.domain.com/article/the-bp-oil-spill-one-year-later/19918396/2011/04/20/
my original rewrite is:
RewriteRule ^article/([^/]+)/([0-999999999]+)/([0-99999999]+)/?$ /index.php?a=show-post&slug=$1&articleid=$2&date=$3 [QSA,L]
and I am trying to do away with this to a pattern which is like:
domain/2011/04/20/article-name-here/
Appreciate your help on this.
Thanks,
L
Are you after something like this:
URL Example: blah/20080101/
RewriteRule ^blah/([0-9]{0,4})([0-9]{0,2})([0-9]{0,2})/$ /blah/$1/$2/$3/
Would output:
/blah/2008/01/01/
Current/Old: /article/the-bp-oil-spill-one-year-later/19918396/20110420/
RewriteRule ^article/([^/]+)/([0-999999999]+)/([0-99999999]+)/?$ /index.php?a=show-post&slug=$1&articleid=$2&date=$3 [QSA,L]
Desired/New: 2011/04/20/article-name-here/
RewriteRule ^article/([^/]+)/([^/]+)/([0-9]{0,4})([0-9]{0,2})([0-9]{0,2})/?$ /$3/$4/$5/$1 [R=301,QSA,L]
This will allow existing links to your current/old style urls to be redirected (permanently) to your new style urls. This assumes that your php app doesn't need to also have the articleid passed to it - note that articleid is not in your new/desired URL.

Resources