Get the language from URL with .htaccess - .htaccess

I have several languages on my site.
At the root I have a index.php. It's my homepage.
I want this type of url : domain.com/fr/ or domain.com/en/
Witch must be the same as domain.com/index.php?lang=fr or domain.com/index.php?lang=en.
So I use this .htaccess rule:
# Redirect to a page (xxxxx.com/index.php?lang=fr)
RewriteRule ^(fr|en|es|cn|ar)/index$ index.php?lang=$1 [L]
But it don't work.
Could you please help me with that ?
Thanks.

You can use:
RewriteEngine On
RewriteRule ^(fr|en|es|cn|ar)/$ index.php?lang=$1 [L]

Related

Redirect already SEO friendly URL to another SEO friendly URL externally using .htaccess

Part of my .htaccess code is as follows:
RewriteRule ^([A-Z]+)*$ nextlevels_state.php?state=$1 [L]
RewriteRule ^([A-Z]+)*/([0-9]+)$ nextlevels_state.php?state=$1&page=$2 [NC]
This basically redirects URL, example.com/XX to example.com/nextlevels_state.php?state=XX and example.com/XX/1 to example.com/nextlevels_state.php?state=XX&page=1 (internally).
Now, I would like to change the URL structure of the URL from example.com/XX to example.com/nextlevels/XX and example.com/XX/1 to example.com/nextlevels/XX/1
and I tried changing .htaccess to as follows:
RewriteRule ^nextlevels/([A-Z]+)*$ nextlevels_state.php?state=$1 [L]
RewriteRule ^nextlevels/([A-Z]+)*/([0-9]+)$ nextlevels_state.php?state=$1&page=$2 [NC]
However, as the site urls are already indexed in search engines, I would like to know a way to redirect all the traffic from example.com/XX to example.com/nextlevels/XX (externally) using .htaccess .
Please guide me in this regard. Thank you community :)
Could you please try following, written and tested with shown samples only(improving your already done attempts here). Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rule for redirect to url example.com/nextlevels/XX.
RewriteRule ^([A-Z]+)$ nextlevels/$1 [R=301]
RewriteRule ^nextlevels/([A-Z]+)/?$ nextlevels_state.php?state=$1 [NC,L]
##Rule for redirect to url example.com/nextlevels/XX/1.
RewriteRule ^([A-Z]+)*/([0-9]+)$ nextlevels/$1/$2 [R=301]
RewriteRule ^nextlevels/([A-Z]+)*/([0-9]+)$ nextlevels_state.php?state=$1&page=$2 [NC,L]

Pagination Redirect with .htaccess

I want to redirect the /page/2 style pagination pages to ?page=2 and to the others in this format.
So any urls like these:
https://www.example.com/dogs/page/2
https://www.example.com/horses/videos/page/3
https://www.example.com/cats/photos/page/4
will be redirected to:
https://www.example.com/dogs?page=2
https://www.example.com/horses/videos?page=3
https://www.example.com/cats/photos?page=4
Could you please try following, written based on your shown samples. Please make sure you clear your browser cache before testing these URLs.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/(.*?)/([^/]*)/([^/]*)/?$
RewriteRule ^(.*)$ %1?%2=%3 [L]
You may use this rule:
RewriteEngine On
RewriteRule ^(.+)/page/(\d+)/?$ $1?page=$2 [L,NC,QSA]

how to Rewrite Single Page url in .htaccess

I need to rewrite only 1 specific URL
from
http://www.domainname.com/index.php?route=payment/axis/callback
to
http://www.domainname.com/payment/axis/callback
I tried these two from stack overflow, I don't know why its not working
1st one :
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?route=payment/axis/callback [NC,L]
2nd one :
RewriteRule ^index.php?route=payment/axis/callback payment/axis/callback [L]
Try this:
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
See the full page here.
Hope it helps!
I wouldn't use .htaccess with RewriteRule, since there are often problems with it. A simple workaround (with PHP redirect):
<?php
if($_GET['route'] == 'payment/axis/callback') {
header("Location: http://www.domainname.com/payment/axis/callback");
}
?>
You can either use h0ch5tr4355's workaround or you can try this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^route=payment/axis/callback$
RewriteCond %{SCRIPT_FILENAME} index.php
RewriteRule (.*) /payment/axis/callback [NC,QSD]
If you instead of a rewrite would like it to redirect to the new url you can add R=301 to the flags of the RewriteRule.

Rewrite http://example.com/test.asp?index=3 to http://example.com/about

I need to Rewrite the old urls generated by ISS to a new system we have build (Joomla).
The url's had to be google friendly. What we want to happen:
Rewrite http://example.com/test.asp?index=3 to http://example.com/about
I've used a few Rewrite's i knew, but they dont work:
RewriteRule ^/test.asp?index=3 / [R=301,L]
RewriteRule ^/test.asp?index(.*)3 / [R=301,L
What pice of code am i missing/doeing wrong?
Kind regards.
You must use QUERY_STRING to check query string.
You can use this code in your htaccess (in document root folder)
RewriteEngine On
RewriteCond %{QUERY_STRING} ^index=3$ [NC]
RewriteRule ^test\.asp$ /about [R=301,L]
Note: put this rule before Joomla's rules

htaccess : rewrite urls

please help to rewrite Urls from
http://www.site.com/index.php?cat=catname
and
http://www.site.com/index.php?id=productname
to be like this
http://www.site.com/catname
and
http://www.site.com/productname
i think it will require php check if the page is cat work with it as cat
or its product work with it as product
in .htaccess file:
RewriteEngine On
RewriteRule ^([0-9a-z\-\_]+)?$ /index.php?id=$1 [L,QSA,NC]
category/product check must be done in index.php...
Or you can add extra slug
RewriteRule ^(category)/([0-9a-z\-\_]+)?$ /index.php?category=$1 [L,QSA,NC]
RewriteRule ^(product)/([0-9a-z\-\_]+)?$ /index.php?product=$1 [L,QSA,NC]
but url will look like http://site.com/category/catname
http://www.site.com/catname and http://www.site.com/productname
The problem with that scheme is that you can't tell whether it's a catalog name or a product name by the URL. As a result, you'll probably want something like this:
http://www.site.com/Catalog/catname and http://www.site.com/Product/productname
Which can then be implemented in a .htaccess file with the following rules:
RewriteEngine On
RewriteRule ^Catalog/(.+)$ /index.php?cat=$1 [L]
RewriteRule ^Product/(.+)$ /index.php?id=$1 [L]

Resources