Redirect sub page to www in htaccess - .htaccess

I have a multi site where the sub pages should redirect from non-www to www.
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^/?$ "https\:\/\/www.example\.com" [R=301,L]
This code above works if you enter https://example.com, then you are forwarded to https://www.example.com. But if you go directly to a subpage such as https://example.com/about, then you aren't redirected to https://www.example.com/about.
How do I write the redirect to fix this?
I have tried different redirects but I can't get them to work.

Well, i your rule you explicitly only match requests to the root path (/) by your pattern ^/?$. That pattern will obviously only match an empty string or a string with a single slash in it and nothing else.
So you need to change that such that the rules gets applied to all requests and that the requested path is preserved in the rewriting step:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^ https://www.example\.com%{REQUEST_URI} [R=301,L]
An alternative you often see is the following. It does not really make sense in your specific situation where you want to rewrite all requests to the "www" variant of your domain. But it might allow for more flexibility if you need to tell apart different requests for specific purposes (like requests to an API versus requests for the UI):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^/?(.*)$ https://www.example\.com/$1 [R=301,L]

Related

301 Redirect in htaccess from NON WWW to https://www with single redirect

I have seen couple of solutions to 301 redirect non www http i.e. http://domain-name to https://www.domain-name but all of them (in my experience) gives 2 redirects.
Either they first redirect from non www to www first and than http to https or in 2nd redirect first and than 1st redirect. One of the example of such code is:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The best solution would be one 301 redirect which takes care of both i.e. non www as well as http part. Can anyone please suggest me the right code.
Best rgds,
Jai
I am using this code and I get only one 301 redirect (to https://www.example.com) when I test http://example.com with httpstatus.io
# ensure www
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The first rule, if your address doesn't start with www will be redirected to the https + www (one redirect)
The second rule applies only if your address starts with a www (because of rule 1) and it doesn't contain https. (one redirect)
Indeed, your domain http://infocera.com is getting 2 redirects.
Using #MrWhite's suggestion about your domain going through Cloudflare: you should go to your domain in Cloudflare -> Rules -> Page Rules and delete any rule you have about redirecting.
I suggest to combine both rules into a single one by combining both conditions:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]
That is simple, thus easy to understand for anyone maintaining the site.
The only drawback would be that you have to specify a fixed http host in the target rule. Which is perfectly fine except if you host multiple domains and hope to find a solution working for all of them at once.
I would generally advise against such an attempt though. Such general rules have nothing to do with an application logic, one should always prefer to implement such general rule in the actual http server's host configuration instead of using distributed configuration files (".htaccess"). That means that you have separate configurations for separate domains, which again allows to place such rule into the separate configuration files while still having only a single actual redirection that is performed.

re-direct a domain to a sub-domain + make it https

this is kinda an odd one:
I need my site to do two things (one of which is already working):
if a user tried to access the domain via HTTP:// it is replaced with https:// - this is for SEO in google and to make the user feel more secure -
the site folder that is used to load the website needs to be the subdomain folder of the site
Oddly the second part of this is working and I figured out - however I'm not sure how to merge these two requests:
HTACCSESS
RewriteEngine on
RewriteCond %{HTTP_HOST} ^trippy\.co\.nz$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.trippy\.co\.nz$
RewriteCond %{REQUEST_URI} !update.trippy.co.nz/
RewriteRule (.*) /update.trippy.co.nz/$1 [L]
But I'm not sure how to make the site display as
https://trippy.co.nz/
I have tried:
RewriteEngine On
RewriteCond %{HTTP_HOST} update\.trippy\.co\.nz [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://update.trippy.co.nz/$1 [R,L]
but then the web address displays as: https://update.trippy.co.nz
and I need to remain as https://trippy.co.nz/
Any help here would really great and I know its a odd situation to be in.
THanks,
Wally
...but then the web address displays as: https://update.trippy.co.nz
You would seem to be redirecting to the subdomain itself, not the subdomain's subdirectory, as you appear to be doing in the first rule. You may also be putting the directives in the wrong order - the external redirect needs to go first - otherwise you are going to expose the subdomain's subdirectory, which does not appear to be the intention.
Try the following instead:
RewriteEngine On
# Redirect HTTP to HTTPS
RewriteCond %{HTTP_HOST} ^(www\.)?trippy\.co\.nz [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]
# Rewrite all requests to the subdomain's subdirectory
RewriteCond %{HTTP_HOST} ^(www\.)?trippy\.co\.nz [NC]
RewriteRule !^update\.trippy\.co\.nz/ /update.trippy.co.nz%{REQUEST_URI} [L]
No need for the extra condition in the 2nd rule block, as the check can be performed directly in the RewriteRule and use the REQUEST_URI server variable instead of the $1 backreference in the substitution string.
That that R by itself is a temporary (302) redirect. You may want to change that to R=301 (permanent) once you have confirmed this is working OK.

htaccess rewrite all subdomains to url path

i need to redirect all request going to any subdomain on domain1.com to domain2.com passing the subdomain part as path, so for example:
sub1.domain1.com -> domain2.com/sub1
this works with my current rewrite rule:
RewriteCond %{HTTP_HOST} ^.+?\.domain1\.com$ [NC]
RewriteRule ^(.*)$ https://domain2.com/$1 [L,R=301]
but when i have a subdomain like the following, it doesn't work:
sub1.sub2.domain1.com -> is redirecting to domain2.com/ without taking the subdomain parts to the path. (domain2.com/sub1.sub2)
how can i get this to work for any combination of subdomain?
I fail to see who any such redirection currently works. The rewriting rule you posted certainly does not do what you describe...
I suggest such an approach to hand over the "subdomain name":
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:(.+)\.)?domain1\.com$ [NC]
RewriteRule ^ https://domain2.com/%1 [L,R=301]
https://sub1.sub2.domain1.com/some/path => https://domain2.com/sub1.sub2
This obviously will ignore whatever path has originally been requested, but that is what you intend, as I understood. If you also want to keep the path and only precede it with the subdomain name, then this variant should point you into the right direction:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:(.+)\.)?domain1\.com$ [NC]
RewriteRule ^/?(.*)$ https://domain2.com/%1/$1 [L,R=301]
https://sub1.sub2.domain1.com/some/path => https://domain2.com/sub1.sub2/some/path
It is a good idea to start out using a 302 redirection and only change that to a 301 once things work as desired. That prevents you from running into caching issues...

.htaccess rewrite url to main domain from subdomain

I have a domain like:
this.that.com
(it can also be accessed from:)
that.com/this
What I would like to do is make it so that the server transfers all requests from the two above url's to:
this.com
I need it so if I type in this.that.com
it will auto transfer to this.com
thanks in advance.
To redirect all requests to this.com, match everything, which is not already this.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^this\.com$
RewriteRule .* http://this.com/$0 [R,L]
If you want to redirect to the main page instead, leave out the URL path
RewriteEngine On
RewriteCond %{HTTP_HOST} !^this\.com$
RewriteRule .* http://this.com/ [R,L]
If everything works as expected, you can switch to R=301 redirection.

Problem setting 301 redirection statements in htaccess

I'm having some trouble setting up a htaccess file. Currently I have 15 domains serving their own website. Now moving this to a single website and domain, I want to serve a htaccess with 301 rules for the old urls. The destination url depends on the domainname of the old url..
E.g.
http://www.previouswebsiteinspanish.com/contacto should be permanently rewritten to http://www.newcentralwebsite.eu/es/contact
I understand that I have to use the RewriteCond but I'm not familiar enough with it to get it working.
RewriteCond %{HTTP_REFERER} !^http://(www\.)?previouswebsiteinspanish/.*$ [NC]
RewriteRule ^([^/.]+)/contacto$ /$1/contact [R=301,L]
Thanks in advance!
You current condition is that the rule will be executed under the condition that the referring page is not on the old domain. That's most likely not what you want to do. :)
You want to check the %{HTTP_HOST} parameter, which contains only the hostname part of the currently requested URL.
Assuming you want to redirect everything on the old domain to the es subfolder, this rule will do it.
RewriteCond %{HTTP_HOST} ^(www\.)?previouswebsiteinspanish.com$ [NC]
RewriteRule ^(.*)$ http://www.newcentralwebsite.eu/es/$1 [R=301,L]
Then you'll have to add similar rules for each old domain.
RewriteCond %{HTTP_HOST} ^(www\.)?previouswebsiteinspanish\.com$ [NC]
RewriteRule ^contacto$ http://www.newcentralwebsite.eu/es/contact [R=301,L]
You need to check HTTP_HOST which is the hostname of the requested site.
From your example the rule I wrote is
RewriteCond %{HTTP_HOST} ^(www\.)?previouswebsiteinspanish\.com$ [NC]
RewriteRule ^contacto$ http://www.newcentralwebsite.eu/es/contact [R=301,L]
Add such rules to your .htaccess.
You have to write such rules for each domain.
In above case you have change in url too, so you may have to handle such special cases too.

Resources