Htaccess rewrite rules to redirect multiple domains - .htaccess

I'm trying to redirect some domains correctly using the htaccess file. Here is what I got:
RewriteCond %{HTTP_HOST} ^special.com$
RewriteRule (.*) https://website.com/some-special-page [L,R=301]
RewriteCond %{HTTP_HOST} !^website.com$
RewriteRule (.*) https://website.com/$1 [L,R=301]
Why is special.com also redirecting to website.com and not to website.com/some-special-page ?
The overall redirect for all the other domains is working fine, however I can't figure out how to have this special case work (and not be overwritten by the second rule. Thanks in advance!

Related

Redirect non www to www sub pages also

in .htaccess Am using :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1/$1/ [R=301,L]
It works fine for main domain i.e. redirects example.com to https://www.example.com
but not works on sub pages like
site.com/blog not redirects to https://www.example.com/blog
also like to add tailing slash '/' after url
https://www.example.com/blog/
how to achieve this?
Thanks in advance!
You should have htaccess rule like this. Make sure you keep these rules at the top of your htaccess file. Please clear your browser cache before testing your URLs.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI}/ [NE,R=301,L]

301 redirection whole domain to new one + few individual pages to new urls

I have olddomain.com - and I want to redirect to newdomain.com with htaccess and 301 - thats easy and working very well for me - if I am redirecting whole domain.
But on the new domain I changed few urls (now they are different then on the previous domain) and I want to redirect whole domain and few specific pages to few specific pages and I dont know how to combine this 2 conditions (redirect whole domain and redirect few specific pages).
This is working for me
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ https://newdomain.com/$1 [R=301,L]
and I would like to add to the code some specific redirects like this but I dont know how to combine it together that it will be working:
Redirect 301 /something/ https://newdomain.com/something-changed-new/
Thank you in advance for a help.
Check this rewrites in top of your .htaccess file
RewriteEngine On
RewriteRule ^something\/$ https://newdomain.com/something-changed-new/ [R=301,L]
RewriteRule ^other\/$ https://newdomain.com/something-changed-other/ [R=301,L]
RewriteRule ^old\/$ https://newdomain.com/new/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule (.*) https://newdomain.com/$1 [L]

htaccess - redirect with condition not working on localhost

I have this redirect in htaccess
I want to redirect
http://localhost/www/foo/bar.php?city=new_york&data=4156
to
http://localhost/www/foo/new_york/?&data=4156
I have create this htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} bar\.php$
RewriteCond %{QUERY_STRING} ^city\=([^&]+)(.*)$
RewriteRule ^(.*)/bar.php$ http://%{HTTP_HOST}/$1/%1/?%2 [L,R=301]
But this is not working on localhost. The URL stays the same, old one. I have tested the rules with https://htaccess.madewithlove.be/ and they seem to be working as expected. So is this just a localhost thing or is the htaccess wrong?
You may use this rule in foo/.htaccess:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^city=([^&]+)&(.*)$ [NC]
RewriteRule ^bar\.php$ /foo/%1/?%2 [L,R=301]
Note that this will redirect to http://localhost/www/foo/new_york/?data=4156 as there is no point of having a & right after ? in URL.

htaccess redirect subdomain to directory

I need help to write proper rewrite rules in my htaccess files.
I need to redirect something like fr.example.com to example.com/fr, because we recently changed the whole website and the multilingual system is managed differently. The structure and the pages too.
I managed to do that successfully with this piece of code:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
My problem now is to write something more specific for pages, for example :
fr.example.com/discover/foo should go to example.com/fr/bar/foo (different path, nothing consistant)
BUT ! example.com/discover/foo should go to example.com/bar/foo (end of the url is the same in both english and french)
Right now, since I have some common 301 redirects, the french urls aren't redirect properly and lead to the english pages. For example that one :
Redirect 301 /discover/foo /bar/otherfoo
Successfully redirects example.com/discover/foo to example.com/bar/otherfoo but also redirects fr.example.com/discover/otherfoo
How can I write two different rules for english and french? I'll have to write a bunch of different rules since everything is very different from the old subdomain to the new directory, I don't mind.
Thanks !
EDIT
Please note that it's for a wordpress installation, and the htaccess starts with :
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
First the these rules:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
should look like this :
RewriteCond %{HTTP_HOST} ^(www\.)?fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
In order to capture bot www & non-www requests for subdomain.
Also this rule :
Redirect 301 /discover/foo /bar/foo
Will capture both requests to domain and sub-domains and using mod_rewrite here is correct not mod_alias so , replace this line with :
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^discover/foo http://example.com/bar/foo [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?(fr)\.example\.com [NC]
RewriteRule ^discover/foo http://example.com/%2/bar/foo [L,R=301]
Note: clear browser cache then test.

.htaccess redirect to subfolder, not working perfectly

I have written a simple redirect condition as:
RewriteCond %{HTTP_HOST} !^my-domain\.com$
RewriteRule ^(.*)$ http://my-domain.com/hu/$1 [L,R=301]
It redirects correctly from www.mysite.com to mysite.com/hu/
But it does not redirect mysite.com to mysite.com/hu/
Please help
You've cleary copied this code without understanding it. This is a typical htaccess to remove the www. part of a domain.
To redirect your homepage to a subfolder, use this code instead :
RewriteCond %{HTTP_HOST} ^(www\.)?my-domain\.com$
RewriteCond %{REQUEST_URI} !^hu/$
RewriteRule ^(.*)$ http://my-domain.com/hu/$1 [L,R=301]

Resources