Htacces - redirecting selected - .htaccess

I have 2 domains (domain1.com and domain2.com).
I want to make such redirects:
a) few specific subpages in domain1.com redirect to specific subpages in domain2.com
b) other subpages from domain1.com redirect to specific subpage in domain2.com .
My htacces dont work propoerly. What i can change in this htacces?
RewriteCond %{HTTP_HOST} ^domain1.com$ [nc]
RewriteRule domain1.com/a http://domain2.com/ssd [R=301,L]
RewriteRule domain1.com/ad http://domain2.com/ssw [R=301,L]
RewriteRule ^(.*)$ http://domain2.com [R=301,L]

You cannot match domain name in RewriteRule.
You can use these rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1\.com$ [NC]
RewriteRule ^a(/.*)?$ http://domain2.com/ssd [R=301,L,NC]
RewriteCond %{HTTP_HOST} ^domain1\.com$ [NC]
RewriteRule ^ad(/.*)?$ http://domain2.com/ssw [R=301,L,NC]
RewriteCond %{HTTP_HOST} ^domain1\.com$ [NC]
RewriteRule ^ http://domain2.com [R=301,L,NC]

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]

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]

.htaccess redirect specific domain ending to folder

i have several domains (same name, but different endings) for the different languages. the .com domain is the main domain and has for each language a directory like /en/
now i want to redirect each of these domains ( e.g. http://example.us/ ) to http://example.com/en/
is this possible with the .htaccess file?
actual i only have a redirection from www.example.com to http://example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
You can do it this way
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} \.(at|ch)$ [NC]
RewriteRule ^(.*)$ http://example.com/de/$1 [L,R=301]
RewriteCond %{HTTP_HOST} \.us$ [NC]
RewriteRule ^(.*)$ http://example.com/en/$1 [L,R=301]
RewriteCond %{HTTP_HOST} \.fr$ [NC]
RewriteRule ^(.*)$ http://example.com/fr/$1 [L,R=301]
But if you have more domains (maybe a lot) you could use RewriteMap

Is there a way to redirect my homepage to a subdirectory?

Is it possible to make it so when a visitor types example.com they end up at example.com/sub/folder/, i.e. the URL bar will display example.com/sub/folder/?
This is my .htaccess file right now:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Try:
RewriteMatch 301 ^/$ /sub/folder
or:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/sub/folder
RewriteRule ^(.*)$ /sub/folder/$1 [L,R=301]

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]

Resources