how to set language - mod-rewrite

how can I set the language (en, da, de etc) with mod_rewrite?
all files are located in the same dir
url to set language is
?set_lang=da
rewrite:
www.domain.com/en/index.php => www.domain.com/index.php?set_lang=en
www.domain.com/en/another_page.php?cat=black => www.domain.com/another_page.php?set_lang=en&cat=black
www.domain.com/da/index.php => www.domain.com/index.php?set_lang=da
www.domain.com/da/another_page.php?cat=black => www.domain.com/another_page.php?set_lang=da&cat=black
EDIT:
my .htaccess looks like this
RewriteCond %{HTTP_HOST} ^domain\.net$ [NC]
RewriteRule ^(.*)$ http://www.domain.net/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.domain\.net$ [NC]
RewriteCond %{REQUEST_URI} !^/_domain\.net/ [NC]
RewriteRule ^(.*)$ /_domain.net/$1 [L]
RewriteCond %{HTTP_HOST} ^(demo|mysql|secure)\.domain\.net$ [NC]
RewriteCond %{REQUEST_URI} !^/_domain\.net/_(demo|mysql|secure)/ [NC]
RewriteRule ^(.*)$ /_domain.net/_%1/$1 [L]
RewriteCond %{HTTP_HOST} domain\.net$ [NC]
RewriteCond %{REQUEST_URI} !^/_domain\.net/ [NC]
RewriteRule ^(.*)$ http://www.domain.net/$1 [L,R=301]

I found a solution
RewriteCond %{HTTP_HOST} domain\.net$ [NC]
RewriteCond %{REQUEST_URI} ^/(da|en)/(.*)(\?%{QUERY_STRING})?$ [NC]
RewriteRule ^(.*)$ /%2?%{QUERY_STRING}&set_lang=%1 [L]

Related

How to point a subdomain to a subfolder

I have an Apache server running ISPConfig. On a domain example.com I am pointing its subdomains to subfolders, e.g. foo.example.com to /sub/foo. This works fine:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/example\.com/
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule ^/(.*)$ /sub/%1/$1 [L]
Now I need to point one specific subdomain into a subfolder's subfolder (bar.example.com into /sub/bar/public to use Laravel). But for some reason, this does not work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^bar\.example\.com/
RewriteRule ^/(.*)$ /sub/bar/public/$1 [L]
RewriteCond %{REQUEST_URI} !^/example\.com/
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule ^/(.*)$ /sub/%1/$1 [L]
The problem was the / at the end of the domain name. If I delete it or put there $, it works.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^bar\.example\.com$
RewriteRule ^/(.*)$ /sub/bar/public/$1 [L]
RewriteCond %{REQUEST_URI} !^/example\.com/
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule ^/(.*)$ /sub/%1/$1 [L]

laravel htaccess force www

I have this .htaccess from laravel 6
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# force www ???
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
and what I want is to force from non-www to www
I have tried several combinations but is not working (i get "redirected you too many times")
What I have tried
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|offs()
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
How to force www ???
Try Also
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.yourwebsite.com/$1 [L,R=301]
RewriteCond %{HTTP} off
RewriteRule ^(.*)$ http://www.yourwebsite.com/$1 [L,R=301]

REQUEST_URI must NOT end on

how can I do this?
RewriteCond %{REQUEST_URI} !payment\.callback\.php$ [NC]
the uri must not end on payment.callback.php
EDIT:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(secure)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !payment\.[^.]\.php$ [NC]
RewriteRule ^(.*)$ https://%1.domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(demo|secure)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/_domain\.com/_secure/ [NC]
RewriteRule ^(.*)$ /_domain.com/_secure/$1 [L]
I don't want to rewrite (http => https) urls to secure.domain.com that ends on payment\.[^.]+\.php$

set multi language

I'm trying to make a mod_rewrite which can set the language
the rewrite:
www.domain.com/da/page.php => www.domain.com/page.php?set_lang=da
So far I have comed up with this:
RewriteCond %{HTTP_HOST} domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/(da|en)/(.*)$ [NC]
RewriteRule ^(.*)$ /%2?set_lang=%1 [L]
but I can't figure out how you can send get vars with it too?
like this:
www.domain.com/da/page.php?cat=black&cow=ugly => www.domain.com/page.php?set_lang=da&cat=black&cow=ugly
yay! found a solution, but I don't know if it could be done in a better way?
RewriteCond %{HTTP_HOST} domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/(da|en)/(.*)(\?%{QUERY_STRING})?$ [NC]
RewriteRule ^(.*)$ /%2?set_lang=%1&%{QUERY_STRING} [L]

How to write the mod_rewrite rules for these requirements?

I need to make rules for mod_rewrite:
from
http://site.kiev.ua/index.php
http://www.site.kiev.ua/
http://www.site.kiev.ua/index.php
to
http://site.kiev.ua/
from
http://site.kiev.ua/catalog/products/941
to
http://site.kiev.ua/catalog/products/941/
from
http://site.kiev.ua/catalog/products/941/index.php
to
http://site.kiev.ua/catalog/products/941/
941 - it may be any category
RewriteEngine On
RewriteBase /
# www to none www
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
# Remove index.php
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.(html?|php)(\?.*)?\ [NC]
RewriteRule ^(.+/)?index\.(html?|php)$ ./$1 [R=301,L]
# Force trailing slashes.
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
# /file.php to /file/
RewriteCond %{REQUEST_URI} ^(/.*[^/])/?$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^([^/]+)/?$ ./$1.php [QSA,L]
Options +FollowSymlinks -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.site\.kiev\.ua$ [NC]
RewriteRule ^(.*)$ http://site.kiev.ua/$1 [L,R=301]

Resources