.htaccess redirect to https with conditions page - regex

I have the following .htaccess
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !(/webservice|/api)
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !\.(gif|jpg|png|css|js|swf)$ index.php [L]
I want all pages to be redirected to https, except /webservice and /api in those two I want it to open with both http and https.
In https requests everything works fine the problem is when I try to access http.
Problems
http://www.local.test/sub/pt-br/webservice redirect to https://www.local.test/index.php
http://www.local.test/sub/pt-br/api redirect to https://www.local.test/index.php
Any idea to make it work?

Change
RewriteCond %{REQUEST_URI} !(/webservice|/api)
to
RewriteCond %{REQUEST_URI} !^(\/webservice|\/api|\/sub\/pt-br\/webservice|\/sub\/pt-br\/api)
or
RewriteCond %{REQUEST_URI} !^\/webservice
RewriteCond %{REQUEST_URI} !^\/sub\/pt-br\/webservice
RewriteCond %{REQUEST_URI} !^\/api
RewriteCond %{REQUEST_URI} !^\/sub\/pt-br\/api

Related

htaccess redirect specific TLD to subfolder while preserving existing root-to-subfolder redirect

I have a domain example.com which hosts an English version of a website on example.com/en-US/ and a French version on example.com/fr-FR/
Until now we have used the English version as default, so we would redirect everybody requesting example.com to example.com/en-US/ using these rules in the .htaccess file:
Redirect 301 /index.html http://examle.com/en/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
So when you request http://exmaple.com in your browser, the server will request the index.html and that will trigger the Rediret to the /en-US/ directory.
Now we got the French example.fr TLD - pointing to the same webspace. I am now looking for a way to reirect the browser request http://example.fr to http://example.com/fr-FR/
I have tried adding the following condition, but it results in some kind of loop adding lots of /en-US/s into the URL.
Please help me, I need a way to redirect
hxxp://example.com --> hxxp://example.com/en-US/
hxxp://example.fr --> hxxp://example.com/fr-FR/
and all people explicitly requesting hxxp://example.com/index.html --> hxxp://example.com/en-US/ too
You can use this rule based on condition based on HTTP_HOST:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^(?:index\.html)?$ /en-US/ [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.fr$ [NC]
RewriteRule ^(?:index\.html)?$ /fr-FR/ [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L,NE]

htaccess redirect from www to non-www

I am new to this.
Code from my .htaccess file goes like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
Redirect 301 /abc/ /abcnew/
I want this to redirect from www to non-www i.e., from http://www.example.com to http://example.com
I copied:
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
this code from here Generic htaccess redirect www to non-www.
I also checked in /etc/apache2/mods-enabled folder on my linux server. There "rewrite.load" this module is present.(I think this might mean that rewrite is enabled on my server, but correct me if I am wrong.)
Redirect 301 /abc/ /abcnew/
and just FYI this above code works fine(its redirecting my old links to new links).
I also tried this.
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Doesn't work for me.
Please help. Thanks in advance...
Edit:
this link I found this. But not sure what should be edited. Can anyone please point out.?
You need to place external (full) redirect rules before internal rewrite ones and also make sure to use mod_rewrite rules only.
Try this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
RewriteRule ^abc/?$ /abcnew/ [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

.htaccess redirect everything to https non-www and 404 to index

I was trying to use .htaccess to redirect any request to https non-www and every 404 to index (previous rules included). This is the end result, but it doesn't work as intended. What am I doing wrong?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ErrorDocument 404 https://mydomain.com/
You can use this code in your .htaccess:
ErrorDocument 404 https://mydomain.com/
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://mydomain.com%{REQUEST_URI} [L,R=301,NE]
This should work:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://mydomain.com/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ / [R=301,L]
The above will redirect all non-https to https and then if the file doesn't exist it is redirect to the index.

Redirect all 404 errors excluding specific 404 urls to homepage using htaccess?

I am currently renovating my website which has 1000s of pages; which requires changes to urls.
I want to 301 permanent redirect ALL 404 errors to website homepage say www.domain.com except some specific 404 URLs.
And I want to 301 permanent redirect those specifc excluded 404 URLs (in no.1) to another URL.
I tried following code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/about-us0.html [NC]
RewriteCond %{REQUEST_URI} !^/contact_us0.html [NC]
RewriteRule ^(.*)$ /$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^about-us0\.html$ "http\:\/\/www\.domain\.com\/about\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^contact-us0\.html$ "http\:\/\/www\.domain\.com\/contact\.html" [R=301,L]
But when I browse to www.domain.com/about-us0.html or www.domain.com/contact_us0.html; google chrome says that "This webpage has a redirect loop".
Is there something I am doing wrong? Any help will be appreciated.
For redirecting without carring query string:
http://domain.com/prod/species.php?action=3&file_id=30
to
http://domain.com/species-30.php
AND
http://domain.com/prod/species.php?action=3&file_id=101
to
http://domain.com/species-101.php
I did something like this (below) which is working fine. Is there any changes that requires in this?
RewriteCond %{QUERY_STRING} ^action=3&file_id=([0-9]+)$ [NC]
RewriteRule ^prod/species\.php$ /species-%1.php? [R=301,L]
Try these rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(about|contact).*?\.html [NC]
RewriteRule ^ / [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^(contact|about)-us0\.html$ http://www.domain.com/$1.html [R=301,L,NC]

.htaccess to redirect certain areas to https

What I'm trying to do is serve the folder /soap OR pages that have /?p=uploads in title over http, the rest of the site over https.
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)(p=uploads)(&|$) [NC,OR]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^ - [E=IS_HTTP:1]
RewriteCond %{SERVER_PORT} 443
RewriteRule ^soap/?(.*) http://www.example.com/soap/$1 [R,L]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/soap
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
It seems to work for the soap folder but not pages with /?p=uploads
Here is a way to do it:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !soap/? [NC]
RewriteCond %{QUERY_STRING} !p=uploads/? [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,QSA]
All URLs are redirected to HTTPS, except those that have /soap or /?p=uploads