Too many redirects with mod_rewrite (at most one redirect?) - apache

I want there to be at most one redirect using my htaccess file. Is that possible?
my htaccess file:
RewriteEngine On
#remove index/index.html
RewriteRule index\.html|index https://example.com/ [R=301,L]
#redirect to non-wwww and https site
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.html [NC]
RewriteRule ^ /%1? [R=301,L]
#remove .html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
If I enter for example http://example.com/foo.html or http://example.com/index then I have two redirects.
If I enter for example http://example.com/ or https://example.com/foo.html then I have only one redirect.
What I want:
I want at most one redirect (see last two examples).
If I navigate to the home page, then I always want the index/index.html to be removed
All http calls should be redirected to https calls
All URL which are called with www, should be called without www
If I navigate to the subpage (https://example.com/foo.html), then only the html should be removed respectively the user can call the page without .html (https://example.com/foo).
Is this possible? Currently sometimes two redirections happen.

Could you please try following, based on your shown samples. Please make sure you clear your browser cache before testing URLs.
RewriteEngine ON
##Apply https and remove www together for all requests here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [NE,L]
##Server files with backend .html files.
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ $1.html [L]
##Deal with home page .html stuff here.
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^(.*)$ index/index.html [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]

Need to redirect 301 a URL in .htaccess file but it adds extra http//?

I am trying to redirect /abc.html to /abc.php but when I did it gives an extra http// and page is not working like http//www.example.de/abc.php don't know from where this HTTP comes.
note: website is not with ssl so domain name is http://example.de
My .htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://example.de/$1 [R=301,L]
RedirectPermanent /tour.html /tour.php
With your shown samples/attempts, could you please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
##To serve home page link.
RewriteRule ^/?$ index.php [L]
RewriteCond %{REQUEST_URI} !^/?$
RewriteRule ^([^.]*)\.html/?$ $1.php [NC,L]

How to htaccess url rewrite multi language

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]

Htaccess Redirects - Combine many rules in one single redirect

I am trying to combine more rules in a single redirect, right now I have many rules and many redirects, like in the picture attached, but for SEO purpose this is not a good behavior.
The story is this: I have an old url, which doesn't exist anymore and a new one, where I want to be redirected. If the requested url doesn't have www. and https then I want to add them.Also if the url has an slash at the end, I want to remove it.
All is working right now, but in many steps.
This is what I have in my .htaccess file:
301 redirect from old url to the new one with www.
RewriteRule ^source1/source2$ https://www.domain.com/destination1/destination2 [L,R=301]
Remove the slash from the end of URL
RewriteCond %{REQUEST_URI} !^/system [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/$ /$1 [R=301,L,QSA]
Redirect to https
#-----------------redirect to https-----------------
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www.domain.com(.*)$ [NC]
RewriteRule (.*) https://www.domain.com%{REQUEST_URI} [L,R=301]
I use all of these rules inside this directive:
<IfModule mod_rewrite.c>
Have it like this to avoid multiple 301 for SEO purpose:
# specific redirects
RewriteRule ^source1/source2/?$ https://www.domain.com/destination1/destination2 [L,R=301,NC]
# Remove the slash from the end of URL
RewriteCond %{REQUEST_URI} !^/system [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ https://www.domain.com/$1 [R=301,L,NE]
#-----------------redirect to https-----------------
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [L,R=301,NE]
Make sure to clear your browser cache completely before testing this change.

https rules for htaccess including www redirect and removal of ".html" extensions

Currently my .htaccess looks like this and works perfect for http.
It redirects to www. and removes the .html file extension.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
I have tried the .htaccess from this answer but still the site in question is completely messed up. E.g https://example.com/work shows a 404.
Also all images that are linked in the source code with /img/example-01.jpg do not show.
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Can you please help me with getting a .htaccess file that:
1.
Redirects to https
2.
Redirects to www. subdomain
3.
Removes .html from file extension so that example.com/work shows the work.html page.
Thank you for your help.
If your current .htaccess works fine, this modification should do:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{REQUEST_SCHEME} !https
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]