How to redir subfolders to different domain than root domain - .htaccess

IHi, I'm to this, I've read and tried all tips I've found here, but it's still not working the way I need.
I'm using htaccess to redir domain and it's structure to new domain:
`rewritecond %{http_host} ^www.olddomain.com [nc]
rewriterule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc]`
It redirs all subfolders e.g. www.olddomain.com/contact/ to www.newdomain.com/contact/ etc., so far so good, BUT I need to redir the mainpage, the one page only, to different URL:
from www.olddomain.com to www.newdomain.com/about-old/
All I've tried this:
`Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^www.olddomain.com [nc]
rewriterule ^(.*)$ http://www.newdomain.com/about-old/ [r=301,nc,L]
rewritecond %{http_host} ^www.olddomain.com [nc]
rewriterule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc]`
but it just redirs EVERYTHING to www.newdomain.com/about-old/
Any ideas, please?
Thanks you

To match home page pattern you need is:
^/?$
So your full code will be:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^(www\.)?olddomain\.com$ [nc]
rewriterule ^/?$ http://www.newdomain.com/about-old/ [R=301,L]
rewritecond %{http_host} ^(www\.)?olddomain\.com$ [nc]
rewriterule ^(.+)$ http://www.newdomain.com/$1 [R=301,L,NE]

Related

htaccess language rediretion

I have a website, www.thesite.com and an alias www.lesite.com
i need a htaccess redirection:
I would like that, when I go to www.thesite.com/fr, it redirects me to www.lesite.com/fr
Likewise, when I go to www.lesite.com/en, I would like it to redirect me to www.thesite.com/en
I have tried different methods but i only succeed to create infinite loops !----
Options +FollowSymlinks
RewriteRule ^fr$ http://dev.mariage.ch/fr/ [L]
RewriteRule ^de$ http://dev.hortzeit.ch/de/ [L]
OR
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch\/de\/
RewriteRule (.*) http://dev.hortzeit.ch/de$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch\/fr\/
RewriteRule (.*) http://dev.mariage.ch/fr$1 [R=301,L]
You can't use path as part of RewriteCond for HTTP_HOST
instead of dev.hortzeit.ch/de you must use just host part dev.hortzeit.ch
RewriteEngine On
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch [NC]
RewriteRule ^de(/?.*)$ http://dev.hortzeit.ch/de$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch [NC]
RewriteRule ^fr(/?.*)$ http://dev.mariage.ch/fr$1 [R=301,NC,L]

Rewrite not working when "www" in URL

I used to have a website at www.roboticsguy.com, which I moved over to www.foxytronics.com. I want to redirect all requests from the old site to the new one. Here is my .htaccess file:
Options +FollowSymlinks
RewriteEngine on
redirect / http://www.foxytronics.com
rewritecond %{http_host} ^roboticsguy.com [nc]
rewriterule ^(.*)$ http://www.foxytronics.com/$1 [r=301,nc]
This URL works:
roboticsguy.com/test/
This one doesn't:
www.roboticsguy.com/test/
What's the problem with the rewrite and how should I fix it?
The line rewritecond %{http_host} ^roboticsguy.com [nc] means "only do the next bit if the domain name is roboticsguy.com". www.roboticsguy.com is not the same as roboticsguy.com.
rewritecond %{http_host} ^roboticsguy.com [nc]
rewriterule ^(.*)$ http://www.foxytronics.com/$1 [r=301,nc]
rewritecond %{http_host} ^www.roboticsguy.com [nc]
rewriterule ^(.*)$ http://www.foxytronics.com/$1 [r=301,nc]
or
rewritecond %{http_host} ^(www.)?roboticsguy.com [nc]
rewriterule ^(.*)$ http://www.foxytronics.com/$1 [r=301,nc]
should work.

Redirect IP to domain

Google search results are showing my pages as (ip)/mypage.html instead of https://www.mydomain.com/mypage.html. I believe the solution is to redirect the ip's to the domain. I've found many, very similar ways to do this, but none of them are working for me. I have an existing rule that redirects http to https. This is what my .htaccess file currently looks like:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^11\.11\.11\.111$ [NC]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^([a-z.]+)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
What am I doing wrong?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^111\.111\.111\.111
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]
Alter "111" to your IP
Your 2 rewrite conditions clash. They require http_host to be 11.11.11.111 and to be *.mydomain.com, at the same time. Just add an or like so:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^11\.11\.11\.111$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
update to willian's answer. you just need to replace the domain name (your-domain.io) from this snippet.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$
RewriteRule (.*) https://your-domain.io/$1 [R=301,L]
Hi there we had requirements to do this as well due to a trigger index in the main pub_html folder. These rules should mask the IP to the http (or https if you switch em), make non-www into www. This should also preserve subdomains. This is for (1) simple site sitting in the pub_html, so test with care if you have subdomain triggers or whatever else in your htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ https://www.exampledomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^50\.28\.55\.76$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?exampledomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ http://www.exampledomain.com/$1 [R=301,L]
Hope it helps and works for ya'll. Thanks for the thoughts.

How to use "NOT" operator on .htaccess redirection?

I'm using .htaccess to redirect users from multiple old domains to a new domain. Something like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} oldsite1.com
RewriteRule ^(.*)$ http://newsite.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} oldsite2.com
RewriteRule ^(.*)$ http://newsite.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} oldsite3.com
RewriteRule ^(.*)$ http://newsite.com%{REQUEST_URI} [R=301,L]
However, I'd like to know if I could use the NOT operator to redirect any domain except newsite.com, like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond != %{HTTP_HOST} http://newsite.com
RewriteRule ^(.*)$ http://newsite.com%{REQUEST_URI} [R=301,L]
Thank you very much!
Official manual: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond
You have placed != in wrong place PLUS %{HTTP_HOST} contains domain name only so no protocol should be there:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !=newsite.com
RewriteRule ^(.*)$ http://newsite.com%{REQUEST_URI} [R=301,L]

Redirect the non-www URL to the www URL

I have referred all the related questions and tried the answer given, but it's not working for my site.
In my .htaccess file, I have written below code to redirect non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Are there any settings in Joomla that need to be set so it will use the .htaccess file?
This is what I use -
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
The differences are minor but should make it work. This works completely outside the scope of Joomla, you shouldn't have to do anything to Joomla for this to work.
You can put the following into your .htaccess to do this:
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
this works for me all the time

Resources