Domain .htaccess redirect issue - apache

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.

Related

Redirect only HTTP subdomain to HTTPS subdomain in htaccess

How do you redirect only the HTTP subdomain to HTTPS subdomain in .htaccess? The main site is in WordPress and already redirected to HTTPS with a plugin, but the subdomain is a PHP created site. I have looked and not seen a conclusive solution to this on here.
I copy and pasted this suggested code but it did nothing:
#Redirect Subdomain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This would create a redirect loop (if executed at all). In order to redirect from HTTP to HTTPS you need to first check you are not already on HTTPS (otherwise you will get a redirect loop).
These directives would need to go in the .htaccess in the document root of your subdomain. Or at the top of your root (WordPress) .htaccess file if the subdomain points to the document root of the main site. The important thing is that the redirect must go before the WordPress front-controller.
This also assumes your SSL cert is installed directly on your application server and not a proxy.
Try the following:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This checks that HTTPS does not contain "on" and the subdomain is being requested before redirecting to HTTPS on the same host.
Although if WP is simply redirecting the main domain, then you could do all this in .htaccess and simply remove the condition that checks against HTTP_HOST. Although if you have other subdomains that should not be redirected to HTTPS then alter the CondPattern to match just the subdomain or main domain (and www subdomain). For example:
RewriteCond %{HTTP_HOST} ^((subdomain|www)\.)?example\.com [NC]
Please try one of the followings:
you need to edit
Redirect "/" "https://yourwebsite.com/"
OR
you dont need to edit following
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]

Redirect root of website only with apache and .htaccess

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]

.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]

Redirect a.com/a.html to b.com/a.html

I need to redirect a.com/a.html to b.com/a.html htaccess Apache. I have several subdomains that are not being redirected to the b.com(which is fine). and the example below is only the first domain that needs to be redirected. Three other domains are needed to be redirected to b.com as well with all of their subpages as a.com/a.html to b.com/a.html. Any hints?
RewriteCond %{HTTP_HOST} ^(www\.)?nexus21\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^tvlift\.com$ [NC]
RewriteRule ^ http://www.tvlift.com%{REQUEST_URI} [L,NE,R=301]
I would do each subdomain redirect separately. That way it is cleaner and more maintainable. If you are redirecting few known files then name them directly.
RewriteEngine on
RewriteRule "^/foo\.html$" "bar.html" [R]
If there are lots of file or you will be adding in future then use a pattern
RewriteEngine on
RewriteRule "^/docs/(.+)" "http://new.example.com/docs/$1" [R,L]
See this page
http://httpd.apache.org

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.