Redirect root of website only with apache and .htaccess - apache

I am looking to redirect only the root of my domain to another domain with apache + .htaccess - However I would like to maintain current directories.
Example:
http://www.domain-one.com/ would redirect to http://www.domain-two.com/
However anything after the / would be ignored, examples:
http://www.domain-one.com/contact
http://www.domain-one.com/?param=1

You can use this rule in the site root .htaccess of domain-one:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain-one\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?$ http://www.domain-two.com/ [L,R=301]

Related

.htaccess redirect chain generated by multiple redirects

Wondering if you can help me?
I'm working on a site at the moment, and the following is in the .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.co.uk [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
RewriteRule ^$ http://www.example/folder-name [R=301,L]
As I see it, this works in the following manner:
root domain rewrites using 301 to www. and then an additional 301 redirect to the /folder-name.
Ideally, I'd like to remove the middle redirect (i.e from root domain to www. and keep just the redirect from root domain to /folder-name.
NB. there is a subdomain, let's call that products.example.co.uk - and access would be needed here.
Is this possible? my .htaccess skills are not particularly tight so any help gratefully received!
This should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/folder-name/$1 [L,R=301]
Ideally, I'd like to remove the middle redirect (i.e from root domain to www. and keep just the redirect from root domain to /folder-name.
This should do it :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example.co.uk [NC]
RewriteRule ^((?!folder).*)$ http://www.example.co.uk/folder/$1 [L,R=301]

htaccess redirect root only without changing url

i have 2 sites:
www.test1.com
and
www.test2.com
With the below code i can redirect from test1.com to test2.com without changing the url
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test1.com
RewriteRule ^(.*) http://test2.com/$1 [P]
But if i try to access a folder in test1 for example test1.com/folder it also redirects.
So how i can only redirect the root folder but still without changing the url but be able to access a subfolder in my main domain?
Just change your regex to allow only landing page:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test1\.com$ [NC]
RewriteRule ^/?$ http://test2.com [P]

Redirecting domain without WWW to the same with WWW

I'm trying to redirect a domain without WWW to the same domain with WWW at the same path with Apache.
So my .htaccess is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain\.com\.ar$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com.ar/$1 [R=301,L]
But the redirection, for example, by requesting "http://mydomain.com.ar/myimage.jpg" results on this URL: "http://www.mydomain.com.ar/mydomain.com.ar/myimage.jpg".
What am I doing wrong?
EDIT: Sorry, but was my problem, I am pointing domain in cPanel to the subdirectory public_html/mydomain.com.ar (public_html is also accessible), using the same .htaccess but in that subdirectory actually works.
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.[a-z-]+\.com.ar [NC]
RewriteRule (.*)$ http://www.mydomain.com.ar/$1 [R=301,L]
</IfModule>
I have employed something similar for many years.

Domain .htaccess redirect issue

I'm trying to create some redirects with .htaccess but I never manage to get it fully functional. Maybe someone here can help me.
What I need is:
http://domain.se and http://domain.com to redirect to http://www.domain.com.
I also need http://domain.se/somefolder, http://domain.com/somefolder as well as http://www.domain.se/somefolder to redirect to http://www.domain.com/folder.
I've tried to accomplish this myself but all I end up with is errors about data not being sent.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# folder rewrite
RewriteRule ^somefolder$ folder [L]
# domain redirect
RewriteCond %{HTTP_HOST} =domain.com [OR]
RewriteCond %{HTTP_HOST} =domain.se
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
This is to be placed in .htaccess file in website root folder. If placed elsewhere some tweaking may be required.
First rule will rewrite (internal redirect) requests to /somefolder to /folder. If you need this to be 301 Permanent Redirect, then replace [L] by [R=301,L]
Second rule will do domain redirect job. This rule will ONLY redirect if domain is domain.com or domain.se. If you want to have redirect from ANY domain name (that your webserver is configured can serve) to www.domain.com then replace those 2 RewriteCond lines with this one: RewriteCond %{HTTP_HOST} !=www.domain.com.
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
That should meet all of your requirements. All requests that are not www.domain.com will be redirected to that domain, with the request URI intact.

How to redirect non-www to www URL's using htaccess?

I have a website say http://www.example.com/ in the root of my website, I have added .htaccess file to redirect any request of http://example.com/ to http://www.example.com/
Recently I have created a new section "Videos" so the URL for videos is http://www.example.com/videos/ . In this video folder I have another htaccess file which is performing rewriting for video entries. When I am trying to access http://example.com/videos/ then its not redirecting me to http://www.example.com/videos/
I think .htacces is not inheriting the previous rules from the parent directory. Can anyone please tell me what can be the rule I can add in the .htaccess file of /videos/ folder so that any request for http://example.com/videos/ will be redirected to http://www.example.com/videos/ URL.
This is a more generic solution, because it can be used with any domain name without having to specify the specific domain name in each .htaccess:
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The contrary is also possible (www to non-www):
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I think it may just be that your existing rule is too strict, and is not getting triggered in your subdirectory because of this. Something like this should work site-wide:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]
This is Best Solutions to redirect non-www to www URL's using htaccess. using this code in htaccess file and check url.
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
I recommend everyone to stick with the below method it will only redirect the main domain only not any other sub-directory or sub-domain in your host.
# Redirect non-www to www only for main domain
# recommended if you have any/some sub-domain(s)
RewriteEngine on
RewriteBase /
# Replace yoursite and .tld respectively
RewriteCond %{HTTP_HOST} ^yoursite\.tld$
# Replace yoursite.com
RewriteRule ^(.*) http://www.yoursite.com/$1 [R=301]
Use this method if you are really sure and I hope you will that you won't ever use sub-domain with your website i.e. subdomain.yoursite.com or whatever. Than you're welcome to use the below method.
"Saying Again make sure if you really want to use this method"
# Redirect all non-www to www including subdomain(s)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The below method will redirect all www url to non-www including your sub-domains although you cannot use www.subdirecty.yoursite.com it will prompt you an error with 500.
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]