Htaccess rewrite url and remove part from url - .htaccess

Hi I'm trying to do the following.
When the domain is accessed through http it should redirect to https.
When domain contains www it should redirect to https
When domain contains language abbreviation it should remove it.
Possible urls:
1: http://www.example.com/en/how-did-it-all-start
2: http://example.com/en/how-did-it-all-start
3: http://www.example.com/how-did-it-all-start
These domains should redirect with one 301 redirect to
https://example.com/how-did-it-all-start
I have tried numerous things but nothing seams to work when I'm trying to combine it with the www. It now only works for http urls, when I add the www to it, it breaks.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{REQUEST_URI} ^en/(.*)$ [NC]
RewriteRule ^ https://%1%/%2% [R=301,L]
I don't know how to properly combine those to conditions without breaking it.
Tried to do in different ways but until now no success.
Any help would be appreciated.
Thanks in advance.

You need to have [OR] clauses in your conditional with optional matches to handle all 3 cases:
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(?:en/)?(.*)$ https://%1/$1 [R=301,L,NE]

Related

htaccess redirect subfolder to root and force https

I'm trying to redirect all URLs with a subfolder to the root, but also force https and www
So, for example, I want to redirect:
example.com/shop/category/product
www.example.com/shop/category/product
example.com/SHOP/category/product
to
https://www.example.com/category/product
I've tried the following code
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1
but this doesn't redirect the subfolder, and if I add the subfolder as a previous rule like
RewriteRule ^shop/(.*)$ /$1 [R=301,NC,L]
It just doesn't work.
One thing to point out - I must also make sure that any other requests to the site (with or without the subfolder) are being redirected to the https www version.
Can anybody point me in the right direction or help me solve this? TIA
You can use this single rule for this redirect at top of your .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{REQUEST_URI} ^/shop/ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(?:shop/)?.*$ https://www.%1/$0 [R=301,L,NC,NE]
# rest of your rules go below
Make sure to clear browser cache or use a new browser for testing.

Complete domain redirect (301) - All version to https://example.com

Looking to redirect an entire domain to its https://example.com version for any variants that are requested (http://, https://www., and www.) in the most SEO friendly way possible.
(I am aware that there are a lot of similar Q/A's, but they seem to be specific so I decided to start with a clean post).
I am currently using this:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
But I have been told that the redirect is not 301, and it also does not redirect the https://www. version.
Is there a simple way to do this all in one go?
Thank you in advance!
Take care
You can use a single rule in your site root .htaccess for this:
RewriteEngine On
# remove www and turn on https in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Use a new browser to test the change.

Redirect all URLs to no-www https

I would like to forward all requests to a domain via .htaccess.
Everything to: https://domain.tdl
No-www
https
I have tried many spelling, but it has not worked for completely.
I think the following single set of rule-set should work:
RewriteEngine On
RewriteCond %{HTTPS} off [NC,OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://domain.tld%{REQUEST_URI} [R=301,L]

.htaccess redirect http to https for only some addon domains

I have a multiple addon domains on my hosting account. I would like to redirect non-https to https for the main domain AND ONLY ONE of the addon domains.
The problem I am experiencing is www.firstaddondomain.com is not redirecting to https://www.firstaddondomain.com. Instead it does not appear to be redirecting at all. It stays at www.firstaddondomain.com.
Note: I do not want to redirect all http to https. I have another addon domain that I do not want redirected to https.
Here is what my .htaccess file looks like:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?maindomain\.com$ [NC]
RewriteRule ^$ https://www.maindomain.com/$1 [R,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?firstaddondomain\.com$ [NC]
RewriteRule ^$ https://www.firstaddondomain.com/$1 [R,L]
UPDATE:
Thanks for your answer, anubhava. My first addon domain is actually a .org domain, so my updated .htaccess file is slightly different from your answer.
Here is my updated .htaccess file
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?firstaddondomain\.org$ [NC]
RewriteRule ^ https://www.firstaddondomain.org%{REQUEST_URI} [R=302,L,NE]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?maindomain\.com$ [NC]
RewriteRule ^ https://www.maindomain.com%{REQUEST_URI} [R=302,L,NE]
Your regex isn't capturing $1
Both rules can be combined into one
You can use:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(maindomain|firstaddondomain)\.com$ [NC]
RewriteRule ^ https://www.%1.com%{REQUEST_URI} [R=302,L,NE]
Make sure this rule is your very first rule and placed in DocumentRoot/.htaccess of both domains.
May vary depending on your hosting conditions, however in Godaddy shared hosting environments & withOUT a Wildcard SSL, ie- a single domain SSL cert., This is the only config that managed to exclude an add-on domain from a force HTTPS redirect:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?ADDON_DOMAIN\.com$ [NC]
RewriteRule .* - [L]# This is essential in order to ensure no further conditions are applied after navigating to the add-on domain #
RewriteCond %{HTTPS} off# Now that you've defined your exclusion AND made certain its logic is independent from all other URI Requests, Must re-establish this condition once more #
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

.htaccess force non-www to www with single page exception

We've successfully forced all pages in the site to use WWW using the following code:
##### Redirect non-www to www -- BEGIN
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/TEST_SITE/$1 [R=301,L]
##### Redirect non-www to www -- END
But our registration page is an exception that has to use a URL that doesn't include the WWW. Two days of testing and research and the only thing I can produce are errors and infinite loops. Does anyone have a suggestion for forcing all pages to WWW except this one page?
http://mysite.com/TEST_SITE/component/users/?view=registration
It seems like it should use a simple redirect before the general WWW redirect, but I can't find a comparable solution in the forums. I'm grateful for any ideas....
Does anyone have a suggestion for forcing all pages to WWW except this one page http://mysite.com/TEST_SITE/component/users/?view=registration Yes you can.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{QUERY_STRING} !view=registration
RewriteRule ^(.*) http://www.%{HTTP_HOST}/TEST_SITE/$1 [R=301,L]
Just add a new condition.
#strip www from registration page
RewriteCond %{HTTP_HOST} ^(www\.)(.+)$
RewriteCond %{QUERY_STRING} ^view=registration$ [NC]
RewriteRule ^TEST_SITE/component/users/$ http://%2%{REQUEST_URI} [L,R=301,NC]
# prevent adding www to registration page
RewriteCond %{QUERY_STRING} ^view=registration$ [NC]
RewriteRule ^TEST_SITE/component/users/$ %{REQUEST_URI} [L,NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/TEST_SITE/$1 [R=301,L]

Resources