htacces ad redirect - is this right? - regex

I need to redirect all my users to another site keeping the current page structure in order to redirect them to the right page on the new domain.
My goal is redirect only the main domain, not the subdomain so I create this snippet but I'm not sure this is right:
RewriteCond %{HTTP_HOST} domain.com
RewriteRule /(.*) http://otherdomain.com [R=301,L]
I already have this in my htaccess that redirect users from www to non-www site:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
How can I make sure that if a user comes from www.domain.com he will be redirect to the new domain?

You can place new rule just below earlier rule like this:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule (.*) http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule (.*) http://otherdomain.com/$1 [R=301,L]
However if there is only one domain hosted then you can replace those 2 rules by this single rule:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule (.*) http://otherdomain.com/$1 [R=301,L]

Related

htaccess remove segment from url and redirect to https and www

I am deploying a Craft CMS site to a shared hosting account using Deployer.
The latest deployment is accessible from domain.com/current/public
My .htaccess file looks like the following, which strips current/public from the url and forces https:
RewriteEngine on
RewriteRule ^(.*)$ current/public/$1
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
I now need to also redirect all urls to use www
How can I adjust my .htaccess to force www on all urls?
*** UPDATE ***
I have managed to solve the above with the following:
RewriteEngine on
RewriteRule ^(.*)$ current/public/$1
RewriteCond %{HTTP:X-Forwarded-Proto} !=https [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+) [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
When I go to example.com/admin it redirects to example.com/current/public/admin. How can I adjust my htaccess file to remove 'current/public' from admin urls?
Your http->https and www adding rule should be top most rule so that it works on original URI instead of a rewritten URI due to other rule.
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !=https [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+) [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(.*)$ current/public/$1 [L]
Make sure to clear browser cache before testing this rule.
Also add this redirect rule in /current/public/.htaccess:
RewriteCond %{THE_REQUEST} /current/public/(\S+) [NC]
RewriteRule ^ /%1 [R=301,L,NE]

htaccess redirect only root domain

I would like to redirect my domain name from one site to another. I only want the rule to be applied if no other subpages are specified
EG:
www.example.com
would get redirected
www.example.com/folder/page.php
would not get redirected
The code I have does a catch all and that is not what I want
RewriteCond %{HTTP_HOST} ^oldsite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.com [NC]
RewriteRule ^(.*)$ http://newsite.com/folder/$1 [L,R=301]
You can use:
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com [NC]
RewriteRule ^/?$ http://newsite.com/folder/ [L,R=301]

apache .htaccess ssl to only one site

I have an .htaccess file for some domains sitting on same folder but leads to different content that is determined by php according to the domain name.
I want only one of them to redirect all requests to ssl domain.
How that's can be done?
When I use redirect rules it transfer all domains:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%mydomain.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%mydomain.com%{REQUEST_URI} [L,R=301]
If you want to skip mydomain.com from this http->https redirection:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?mydomain\.com$
RewriteCond %{HTTPS} off
RewriteRule ^ https://%mydomain.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%mydomain.com%{REQUEST_URI} [L,R=301]

Rewrite all URL requests to https://www.example.com/$1

I have two domains, example.com and example.co.uk, that use the same application on my server.
I would like to rewrite the address of the URL depending on what the user types in. The only URLs I want are https://www.example.com and https://www.example.co.uk
In my .htaccess file I have the following:
# Turn on URL rewriting
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
If I goto http://www.example.com it doesn't add the https, if I goto http://example.com it does. What is the best way of making the ReWriteCond match the www url?
Have your 2 rules like this:
RewriteEngine On
# add www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
# turn on HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
These 2 rules will take care of both of your domains.

HTACCESS Redirect all domain except two files

My actual htaccess redirect my complete old site to my new site, I want to redirect all domain but except two file .php (protectetfile1.php and protectedfile2.php).
This is my htaccess
RewriteEngine On
Rewritecond %{http_host} ^olddomain.org [NC]
Rewriterule ^(.*)$ http://newdomain.net/$1 [L,R=301]
Use this rule:
RewriteEngine On
Rewritecond %{http_host} ^olddomain\.org [NC]
RewriteCond %{REQUEST_URI} !/(protectetfile1|protectetfile2)\.php [NC]
Rewriterule ^(.*)$ http://newdomain.net/$1 [L,R=301]