How to htaccess url rewrite multi language - apache

Can someone help me with the htaccess rewrite url?
I need to convert ?lang=en to /en
To always display the language used in the url
When someone comes to example.com to automatically 301 redirect them
to example.com/en as the main language.
When a user chooses another language to stay on the same page e.g.
example.com/en/contact.php
I have a total of three languages on the site en, de, hr.
The problem with my code is when I go to example.com/contact and select a language, it takes me back to the homepage example.com/en. If I type manually example.com/en/contact it works.
If I am at example.com/en/contact and choose another language e.g. /de url changes to example.com/en/de and a 404 error.
My current htaccess code looks like this
# Convert ?lang=en in /en
RewriteRule ^([a-z]{2})/?$ ?lang=$1 [QSA,L]
RewriteRule ^([bs|en|de]{2})/(.*)$ $2?lang=$1&%{QUERY_STRING} [L,QSA]
# Remove trailing slash from URL
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://example.com/%1 [R=301,L]
# Remove php extension in URL
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

With your shown samples, could you please try following.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !ON
RewriteCond %{HTTP_HOST} ^(?:www\.)reg\.bmi\.id$ [NC]
RewriteRule ^/?$ https://reg.bmi.id [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\scontact/contact\.php [NC]
RewriteRule ^ https://reg.bmi.id [R=301]
RewriteRule ^ contact/contact.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Related

htaccess rewrite if url doesnt have a query string

I have a htaccess that rewrites to /login if conditions aren't met.
It's working fine for urls without query_string.
However I have no clue how to include /reset?query_string to accepted conditions. And want to exclude /reset
/reset?6tdsBMxpeeJEDUvYzwKmLzIusLYLjgMtIj
RewriteEngine On
RewriteBase /index.php
RewriteCond %{REQUEST_URI} !^/login$
RewriteCond %{REQUEST_URI} !^/forgotten$
RewriteRule ^([^\.]+)$ /login [R=301,L,QSD]
RewriteRule ^(/)?$ /login [R=301,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Here is a modified version of your .htaccess that excludes /reset?query:
RewriteEngine On
# /reset with query string
RewriteCond %{QUERY_STRING} .
RewriteRule ^(reset)/?$ $1.php [L,NC]
RewriteCond %{REQUEST_URI} !^/(forgotten|login)$ [NC]
RewriteRule ^([^.]*)$ /login [R=301,L,QSD]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)$ $1.php [L]
Make sure to test it after clearing your browser cache.
With your shown samples and attempts, please try following htaccess rules file. Make sure to clear your browser cache before testing your URLs.
Also I am making use of apache's variable named THE_REQUEST by which we can handle both uri as well as query string.
RewriteEngine ON
RewriteBase /
##Rules for handling rest uri without query string.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^reset/?$ /login? [R=301,L,NC]
##Rules for reset with uri and query string.
RewriteCond %{THE_REQUEST} \s/reset\?\S+\s [NC]
RewriteRule ^ /reset? [R=301,NC]
##Rule for login page's backend rewrite.
RewriteRule ^login/?$ login.php [QSA,NC,L]
##Rule for forgotten page's backend rewrite.
RewriteRule ^forgotten/?$ forgotten.php [QSA,NC,L]
##Rules for non-existing pages should be served with index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

RewriteRule - remove params keys and keep values in address bar

RewriteEngine ON
RewriteRule ^video$ video.php [L]
the above line works
example.com/video is interpret as example.com/video.php
now I need example.com/video?id=5&s=lorem-ipsum
to interpret as example.com/video/5/lorem-ipsum - and vice versa
RewriteCond %{THE_REQUEST} video?id=([^\s&]+)&s=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?id=$1&s=$2 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?/$1/$2 [L,QSA]
doesn't work - in both directions
pls help
With your shown attempts, please try following .htaccess rules file. These rules are assuming you want to infernally rewrite to index.php you can change php file name in 2nd set of rules as per your requirement.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect Rules from here..
##Redirect 301 to example.com/video/5/lorem-ipsum from example.com/video?id=5&s=lorem-ipsum
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/(video)\?id=([^&]*)&s=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
##Internal rewrite for example.com/video?id=5&s=lorem-ipsum
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^video/([^/]*)/(.*)/?$ index.php?id=$1&s=$2 [QSA,NC,L]

redirect loop with mod_rewrite

I'm trying to add a language prefix to my pages through mod_rewrite
eg:
https://example.com/ > https://example.com/en/
and internally it need to translate to `https://example.com/?language=en
I've managed to do it for the base url, but when I try to do it with some other redirect implied, it's always end in an infinite loop.
eg:
https://example.com/product-p-22.html > https://example.com/en/product-p-22.html
and internally became `https://example.com/product_info.php?product_id=22&language=en
here is my actual config:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(en|fr)/
RewriteCond %{REQUEST_URI} .(php|html) [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} !language=
RewriteRule ^(.*)$ en/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/example
RewriteRule ^(.*)/$ index.php?language=$1&%{QUERY_STRING}
RewriteRule ^(.*)/(.*)-p-(.*).html$ product_info.php?products_id=$3&language=$1&%{QUERY_STRING}
I've tried many flags like L, END, DPI or some combination of those, but with no luck.
when I look at the debug logs, it seems to find the right url, but then restart parsing the default url:
pass through /var/www/example/product_info.php
init rewrite engine with requested uri /product-p-22.html
pass through /product-p-22.html
any though on what I'm doing wrong here?
Server version: Apache/2.4.41 (Ubuntu)
Based on your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^en [NC]
RewriteRule ^(.*)$ en/$1 [NC,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php?language=en [NC,QSA,L]
RewriteRule ^([^/]*)/([^-]*)-p-([^.]*)\.html$ product_info.php?products_id=$3&language=$1& [NC,L,QSA]
Have it this way in your site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} !\s/+(en|fr)/ [NC]
RewriteRule ^ /en%{REQUEST_URI} [NC,R=301,L]
RewriteRule ^([a-z]{2})/[^-/]+-p-([^./]+)\.html?$ product_info.php?language=$1&products_id=$2 [L,QSA]
RewriteRule ^([a-z]{2})/$ index.php?language=$1 [QSA,L]

Joomla: Permanently redirect from example.com/index.php/foo/bar to example.com/foo/bar (no index.php in URL)

.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
With this setup the page /foo/bar is accessible under the pretty URL example.com/foo/bar while the ugly URL example.com/index.php/foo/bar is still valid. What I need to achieve is a permanent redirection from example.com/index.php/foo/bar to example.com/foo/bar.
RewriteRule .* index.php [R=301,L] doesn't work.
RewriteRule (.*) index.php/$1 [R=301,L] does the exact opposite, it redirects from example.com/foo/bar to example.com/index.php/foo/bar. Please help me out!
I recommend writing this:
RewriteEngine On
RewriteBase /
# remove index.php from / OR any /dir
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
# redirect rule to remove /index.php/ from start
RewriteRule ^index\.php(/.*) $1 [L,R=301,NC]
RewriteCond %{REQUEST_URI} !^/index\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
In the new rule, we match a pattern using regex ^index\.php(/.*) which matches a URI that starts with /index.php/ followed by zero or more of any characters. (/.*) is our match and the capture group after /index.php which is used as back-reference i.e. $1 (string that we've captured in group #1) in target to get a URI after /index.php as desired.
References:
Apache mod_rewrite Introduction
.htaccess tips and tricks

how to fix htaccess error with multiple rewrite rule - ERR_TOO_MANY_REDIRECTS

why this htaccess has error. it says "to many redirect" please help thanks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://us.domain.com/ae_en/$1 [R=301,L]
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^us\.domain\.com$
RewriteCond %{REQUEST_URI} !^/(ae_en|ae_ar) [NC]
RewriteRule ^(.*)/ae_en/%{REQUEST_URI} [R=301,L]
I cannot try the wrong code, it always appear error
There are several issues with your .htaccess. I would suggest both re-ordering and editing the rules.
This would be a better approach, in my opinion. I'll explain the parts below.
RewriteEngine on
RewriteBase /
Options +FollowSymLinks
# External redirect: Remove www from domain
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# External redirect: Ensure language code is set
RewriteCond %{HTTP_HOST} ^us\.domain\.com$
RewriteCond %{REQUEST_URI} !^/(ae_en|ae_ar) [NC]
RewriteCond %{REQUEST_URI} !^/index.php$ [NC]
RewriteRule ^(.*)$ /ae_en/$1 [R=301,L]
# Internal redirect: Pass everything non-existent to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
Structure
First, general options and settings should come at the top and only once, i.e.
RewriteEngine on
RewriteBase /
Options +FollowSymLinks
Second, I like to get the external URL to show up correctly, so I group all external redirects here - denoted with comments in the above code block.
Third, internal "redirects" follow, meaning mapping the (now correct and final) external URL to the correct scripts.
This structure ensures that the internal scripts are only matched on a URL that will not change when re-run against the .htaccess on the next RewriteRule match.
Omitting external redirect for index.php
I guess you added the index.php redirection handling after adding in the internal redirect, which probably has caused index.php to show up in the browser. So, with the above structure in place, I assume it will not show up again.
Error in last RewriteRule
As Mike Rockett pointed out in the comment above, there also was an error in the last RewriteRule.
Avoid redirect loop
To avoid a redirect loop when rewriting to index.php with the last rule, you have to explicitly exclude index.php from language code redirection, i.e.
RewriteCond %{REQUEST_URI} !^/index.php [NC]