How to redirect between subdomains? - .htaccess

I have site.com with htaccess file. How to write redirect:
from
https://subdomain.site.com/metal/blokiloki/wto-420132015-65/
to
https://subdomain.site.com/metal/material/privodnye-bloki/wto-420132015-65/
Tried:
RewriteCond %{HTTP_HOST} ^(.*)subdomain.site.com/metal/material/privodnye-bloki/wto-420132015-65/$ [NC]
RewriteRule ^(.*)$ http://%1subdomain.site.com/metal/material/privodnye-bloki/wto-420132015-65/ [R=301,L]
UPDATE!
This works:
RewriteRule ^metal/blokiloki/wto-420132015-65/?$
https://%{HTTP_HOST}/metal/material/privodnye-bloki/wto-420132015-65/ [R=301,L]

Related

Redirecting a web folder directory to another htaccess

Sorry this has no doubt been asked multiple times before, I just want clarification that the following code will redirect any url on olddomain.com to the newdomain.com homepage not the equivalent url:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
Also if I wanted any subdomain on olddomain.com eg.subdomain.olddomain.com to go to the homepage of newdomain.com what would I have to do? Can I use a universal selector or would I have to write a condition for each subdomain like so:
RewriteCond %{HTTP_HOST} ^subdomain.olddomain.com$
RewriteRule ^(.*)$ http://subdomain.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.subdomain.olddomain.com$
RewriteRule ^(.*)$ http://subdomain.newdomain.com/$1 [R=301,L]
Both of attempts are not correct as first will redirect:
http://olddomain.com/foobar to http://newdomain.com/foobar
not to the homepage of newdomain. Same is the problem with 2nd rule.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ http://www.newdomain.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.olddomain\.com$ [NC]
RewriteRule ^ http://subdomain.newdomain.com/ [R=301,L]

Redirect www to non-www with htaccess

I try to redirect my website from www to no-www for whole content but I don't know how I get it work, because I've got two rewrite rules already.
This is my htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^meaning-first-name\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^([^/\.]+)/?$ index.php?name=$1 [L]
RewriteRule ^([^/\.]+)?$ index.php?name=$1 [L]
RewriteRule ^voorletter/([^/\.]+)/?$ initial.php?letter=$1 [L]
RewriteRule ^voorletter/([^/\.]+)?$ initial.php?letter=$1 [L]
Now is working:
website.com/John* shows: *index.php?name=John
website.com/initial/J* shows: *initial.php?letter=J
But how can I include redirect for www to non-www to this?
Remove the 2 conditions on top and add this instead:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

Redirect to HTTP non-www to HTTPS www htaccess

I want to redirect from any direction to our site with HTTPS protocol, but some redirects it's not working. I want this:
http://www.site.co TO https://www.site.co
http://site.co TO https://www.site.co
This is my htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
The second rule it's not working. It going to another direction inside our site, and it isn't redirect to HTTPS site.
Try it like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
The only real difference here is that first we redirect from non-WWW to WWW then we check for HTTPS and redirect it.
If it does not work, try this one:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
The answer by Prix works. To make it more dynamic lets use SERVER_NAME and REQUEST_URI instead of a static domain name.
RewriteEngine On
#we replace domain.com/$1 with %{SERVER_NAME}%{REQUEST_URI}.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
#here we dont use www as non www was already redirected to www.
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
Sorry I don't have enough point to comment, but the rule of #prix will bring unneeded redirect.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
You can try http://domain.com/ on GTMETRIX and you will get this message
"Avoid landing page redirects for the following chain of redirected URLs."
http://domain.com/
http://www.domain.com/
https://www.domain.com/
To prevent that, go to the first RewriteRule and add a "s" at the end of http. The new set of rule will look like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
This is http to https and non www to www redirection using .htaccess
If you want to redirect to https and www you can use this code
http to https redirection:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
</IfModule>
Non www to www redirection:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
If you want to use booth functionality place the above all code respectively
In case you are using One.com as your webhost you should use the following code instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have an asp.net website and the host provider is not supporting IIS rewrite module i have tested all this methods and only this code gives woorank.com confirmation maybe useful for another .net developer :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [L,R=301]
This will use for both www or non-www
If you try to open link with www then url redirect to https with www
Example : http://domain.com redirect to https://domain.com
or If you try to open link with non-www then url redirect to https with non-www
Example : http://www.domain.com redirect to https://www.domain.com
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Just put the following in the .htaccess file
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [END,NE,R=permanent]
Try this methode:
# Redirect 301 all to https://www
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.example.com/$1 [L,R=301]
</IfModule>
Why sometimes you don't need code %{HTTP_HOST} and use your domain exact url with www for the last line, because usually the result will have two www like:
https://www.www.yourdomain.com
Btw if you use cloudflare, just create pagerules redirect 301 all to https and www version.
Create pagerules
Fill with
example.com/*
Choose setting forward url then choose 301 and fill with this
https://www.example.com/$1
Save it and all will be redirected to https and www version.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
For example https://example.com should be going to https://www.example.com
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ "https\:\/\/www\.example\.com\/$1" [R=301,L]
Note - you should change example.com to your own domain.

htaccess redirect subdomain to folder and non-www to www

sub.domain.com redirect to www.domain.com/sub redirects using:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteRule ^(.*)$ "http://www.domain.com/sub/$1" [R=301,L]
domain.com/sub to www.domain.com/sub is where I'm stuck.The solutions I've found always drop the /sub/ folder when rewriting the www.
This works:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/sub/$1 [R=301,L]

Using .htaccess to redirect www URLs to non-www for https

Please provide me with .htaccess syntax to do the following:
Redirect from http to https
Redirect from https://www.domain.com.uk to https://domain.com.uk
I tried the following but it didn't work:
RewriteCond %{HTTP_HOST} ^www.(.*)
RewriteRule ^.*$ https://%1/$1 [R=301,L]
Try adding the following to your htaccess file in the root folder of your domain.
RewriteEngine on
RewriteBase /
#if not domain.com.uk then redirect to domaim.com.uk
RewriteCond %{HTTP_HOST} !^domain\.com\.uk$ [NC]
RewriteRule .* http://domain.com.uk%{REQUEST_URI} [L,R=301]
#if not https
RewriteCond %{HTTPS} off
#redirect to https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.com.uk
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://domain.com.uk/$1 [R=301,L]
For me this work, just tested. So http://www.domain.com.uk into https://domain.com.uk .

Resources