Rewrite not working when "www" in URL - .htaccess

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.

Related

Htaccess redirect from subdomain, but not it's folders

I have
sub.domain.com and want it to be redirected to domain.com
But there must not be redirects from e.g. sub.domain.com/folder/ or sub.domain.com/folder/index.html
Everything i've tried does not work...
For example
RewriteEngine on
RewriteCond %{HTTP_HOST} sub.domain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://www.domain.com/ [L,R=301]
You can use this :
RewriteEngine on
RewriteCond %{HTTP_HOST} sub.domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/folder/?$
RewriteCond %{REQUEST_URI} !^/folder/index\.html$
Rewriterule ^(.*)$ http://www.domain.com/ [L,R=301]

Redirect www to non www with subfolder and links

Ok, so i want to redirect all www to non www link,
I want to redirect www.example.com to example.com, and i can do that too using this code.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
But when someone try to access www.example.com/example, that redirects the site to example.com,
I want a code that can redirect www.example.com/example or any subfolder to example.com/example or any subfolder.
Kinda like a wildcard entry, how to do that?
Also if possible force ssl on it too.
Thank You.
If I've understood your question correctly:
www.example.com/dir/anypage.html to example.com/dir/anypage.html (same page)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [nocase]
RewriteRule ^(.*) http://example.com/$1 [last,redirect=301]
OR Use:
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*)$ http://example.com/subfoldername/$1 [R=301,L]
-------- Alternative---------
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(example\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1 [L]
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(/)?$ /subfolder/index.php [L]
To force SSL:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

Three 301 redirect

hope some could help me.
I have a wordpress blog with 3 domains, .com, .es and .net. The .com is the target.
And i want to redirect from non-www to www as well.
The redirect from .es to .com works fine, and .net to .com too. But when i add the redirect from non-www to www is not working and the site doesnt load.
This is my .htaccess file...
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^myblog.com [NC]
RewriteRule ^(.*)$ http://www.myblog.com/$1 [L,R=301,NC]
#RewriteCond %{HTTP_HOST} ^myblog.es$ [OR]
#RewriteCond %{HTTP_HOST} ^www.myblog.es$
#RewriteRule ^(.*)$ "http\:\/\/www\.myblog\.com/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^myblog\.es$ [OR]
RewriteCond %{HTTP_HOST} ^www\.myblog\.es$
RewriteRule ^(.*)$ "http\:\/\/myblog\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^myblog.net [NC,OR]
RewriteCond %{HTTP_HOST} ^www.myblog.net [NC]
RewriteRule ^(.*)$ http://www.myblog.com/$1 [L,R=301,NC]
These rules should suffice for your needs:
RewriteEngine On
# redirect myblog.es and myblog.net (with or without www.) to www.myblog.com
RewriteCond %{HTTP_HOST} ^(www\.)?myblog\.(es|net)$
RewriteRule ^(.*)$ http://www.myblog.com/$1 [R=301,L]
# Force www on myblog.com
RewriteCond %{HTTP_HOST} ^myblog.com [NC]
RewriteRule ^(.*)$ http://www.myblog.com/$1 [L,R=301,NC]

HTACCESS - Redirect multiple parked domains to Different URLs

There are 10 different domains names. I would like to redirect some of them to:
somedomain1.com, somedomain2.com -> somenewdomainONE.com/en
somedomain3.com, somedomain4.com -> somenewdomainTWO2.com/fr
I have tried this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?somedomain1.com$ [NC, OR]
RewriteCond %{HTTP_HOST} ^(www.)?somedomain2.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomainONE.com/en [L,R=301]
RewriteCond %{HTTP_HOST} ^(www.)?somedomain3.com$ [NC, OR]
RewriteCond %{HTTP_HOST} ^(www.)?somedomain4.com$ [NC]
RewriteRule ^(.*)$ http://www.somenewdomainTWO.com/fr [L,R=301]
I'm not sure why, but this redirects all domains to http://www.newdomainONE.com/en even when I try accessing somedomain3 or somedomain4.com. I'm also getting a redirect loop depending on what I try.
You can just use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?(domain1|domain2)\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomainONE.com/en/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?(domain3|domain4)\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomainTWO.com/fr/$1 [L,R=301]
Make sure to test in a new browser to avoid old 301 cache.

Joomla www enforcing in urls with .htaccess, not working as it should

I have a joomla 1.6 website, and I want to enforce a www in the beginning of urls.
I've written the following into htaccess:
RewriteCond %{http_host} ^example\.com [nc]
RewriteRule (.*) http://www.example.com/$1 [r=301,nc]
What it should do is redirect from example.com/happy_bunnies.html to www.example.com/happy_bunnies.html. Instead it redirects to www.example.com/index.php
RewriteCond %{http_host} ^example\.com [nc]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule .* http://www.example.com%1 [r=301,nc,l]
No idea why the first didn't work, but this one does
Have you tried...
RewriteCond %{http_host} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC]

Resources