.htacess existing URL from old domain, redirects to index.php on new domain - apache

Problem
I have a new TYPO3 website on a new domain. All important pages of the old website point to their new counterpart for which I added individual 301 redirects in the .htaccess. Those work fine!
However there are pages which have the exact same URL as the old website, which I cannot add individual redirects for (infinite loop). For example /contact/. When I access http://www.example-old.com/contact/ the page simply redirects to /index.php. Why?
Wishlist
All non-www should be redirected to www (works)
Other domains should be redirected to new (works)
Referrals from the old website, should be redirected to the same URL on the new domain, unless a manual 301 redirect is in place. (does not work)
Current setup
This is the .htacess snippet I'm working with:
<IfModule mod_rewrite.c>
# Enable URL rewriting
RewriteEngine On
RewriteBase /
# Point old domain to new domain
RewriteCond %{HTTP_HOST} ^example-old\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example-new\.com$ [NC]
RewriteRule ^(.*) http://www.example-new.com/$1 [R=301,L]
# Point idle domains to new domain
RewriteCond %{HTTP_HOST} ^/?(?:www\.)?example-01.nl [OR]
RewriteCond %{HTTP_HOST} ^/?(?:www\.)?example-02.de [OR]
RewriteCond %{HTTP_HOST} ^/?(?:www\.)?example-03.eu [OR]
RewriteCond %{HTTP_HOST} ^/?(?:www\.)?example-04.net [OR]
RewriteCond %{HTTP_HOST} ^/?(?:www\.)?example-05.org
RewriteRule ^(.*)$ http://www.example-new.com/$1 [R=301,L]
</IfModule>
I have tried every combination I could find to try and get this to work. What could be going wrong here?
Many Thanx!

Try to remove the leading "/?" and the inner "?:" also escape the last dot from your http host conditions:
RewriteCond %{HTTP_HOST} ^(www\.)?example-01\.nl$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example-02\.de$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example-03\.eu$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example-04\.net$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example-05\.org$
RewriteRule ^(.*)$ http://www.example-new.com/$1 [R=301,L]

If you remove the ^ from the first RewriteCond all request to the old domain will be redirected to the new domain.
If you want the specific redirects to have priority make sure you put them before this one.
# Point old domain to new domain
RewriteCond %{HTTP_HOST} example-old\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example-new\.com$ [NC]
RewriteRule ^(.*) http://www.example-new.com/$1 [R=301,L]

Related

Redirect multiple domains (http https www & non-www) to a new domain using htaccess

I have 3 different domains (with two different TLD) and I would like that all my domains redirect to only one (including all format http https www or non-www) -> https://www.newdomain.com
All my domains :
http://olddomain.net
http://www.olddomain.net
https://olddomain.net
https://www.olddomain.net
http://olddomain2.com
http://www.olddomain2.com
https://olddomain2.com
https://www.olddomain2.com
http://newdomain.com
http://www.newdomain.com
https://newdomain.com
https://www.newdomain.com
So all these domains have to redirect to : https://www.newdomain.com
I have tried many different configurations of my htaccess but I do not handle all the redirections. So far what I found :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.net$ [OR]
RewriteCond %{HTTP_HOST} ^olddomain2\.com$ [OR]
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.newdomain.com/$1 [L,NE,R=301]
But it does not handle all the variations. I think this is confusing Google bot as my new main url is not indexed.
How can I modify my htaccess so it handle all the possibilities ?
It can be done using single redirect rule like this:
RewriteEngine On
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.newdomain\.com$ [NC]
RewriteRule ^ https://www.newdomain.com%{REQUEST_URI} [NE,R=301,L]
Based on your shown samples, could you please try following. Please make sure you clear your browser cache before checking your URLs. I would have checked condition if your HTTP_HOST is NOT equal to your new domain but then I am afraid that it can touch your other domains too which you don't want to redirect to your new domain.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.net [NC]
RewriteRule ^ https://www.newdomain.com%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain2\.com [NC]
RewriteRule ^ https://www.newdomain.com%{REQUEST_URI} [NE,R=301,L]

htaccess isn't redirecting to https

I'm trying to create an htaccess file in the root directory that will also affect subdomain directories (the subdomain works from a directory in the root).
Firstly, will this work if I have one htaccess file in the root? (will it work for subdomains in browsers (as they are actually just directories from root?) ).
The problem that I'm having is that when I try to visit http://demo.example.com, instead of it redirecting to https://demo.example.com it just remains using http://. This also is the case when trying to view http://www.example.com (it doesn't redirect to https).
This is my entire htaccess file as it currently stands. It seems quite bloated to me. Is there a way I can just redirect everything to https, regardless of subdomain / root ?
<IfModule mod_rewrite.c>
RewriteEngine On
# redirect subdomain to https
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.example\.com$ [NC]
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
</IfModule>
Can you try these rules in your site root .htaccess:
RewriteEngine On
# add www and https to main domain
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
# redirect everything to https
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Test it in a new browser or completely clear your browser cache before testing.

htaccess rewrite from http to https

Currently I redirect all http users (www or non-www) of upscfever.com to http://upscfever.com/upsc-fever/index.html
using
RewriteEngine on
RewriteCond %{HTTP_HOST} ^upscfever\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.upscfever\.com$
RewriteRule ^/?$ "http\:\/\/upscfever\.com\/upsc\-fever\/index\.html" [R=301,L]
Now I want all users to shift to https so I modified as follows:
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^upscfever\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.upscfever\.com$
RewriteRule ^/?$ "https\:\/\/upscfever\.com\/upsc\-fever\/index\.html" [R=301,L]
So that all who type upscfever.com OR www.upscfever.com should go to
https://upscfever.com/upsc-fever/index.html - instead
Plus all links should be https. But its not working I get Page not found.
Your server has to setup the https first, depend on hosting vendor, if your hosting is vps you need to setup https for apache, install cert also.
You can find some instruction like this:
https://manual.seafile.com/deploy/https_with_apache.html
https://www.digicert.com/csr-ssl-installation/apache-openssl.htm
I think you want to make 3 different changes:
Change your .htaccess file to redirect requests to root to your custom index irrespective of the HTTPS or HTTP for the original request
RewriteEngine on
RewriteCond %{HTTP_HOST} ^upscfever\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.upscfever\.com$
RewriteRule ^/?$ "https://%{SERVER_NAME}/upsc-fever/index.html" [R,L]
There is no R=301 part here because I'm not sure it is really wise to make permanent such a redirect to an obscure inner URL.
Redirect all other non-HTTPS requests to HTTPS (preserving the rest of the URL):
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^upscfever\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.upscfever\.com$
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
Making this redirect permanent seems pretty safe.
Change all internal links in all of your HTML pages (or whatever backend generates them) to use protocol-relative // prefix or explicitly https:// instead of current http://. Preserve the protocol for the external links as is.
As for troubleshooting, you may use the Network tab of the Chrome DevTools (F12) to see exact server reply (note: enabling "Preserve log" and "Disable cache" flags is useful in such context)
You can do that using a single rule as follows in your site root .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?upscfever\.com$ [NC]
RewriteRule ^/?$ /upsc-fever/index.html [R=301,L]
This will redirect both http and https URLs.
You may try something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I hope below code will do the work for you
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://famebooking.net/$1 [R,L]
just simply add above code in .htaccess below authorization header condition is written under RewriteEngine On
Let me know if that helps.

htaccess http to https with www. Without redirecting sub domain

I have a RewriteRule that redirects my main domain to https://www.sta-games.com which works fine, but when I try to access my subdomain http://files.sta-games.com, it redirects to my main domain.
Heres my redirect rules
#HTTPS Redirection
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Can anyone see the problem?
You need to adjust your rules so that it looks for the whole domain instead of just part of it. Right now you are only looking for the absence of www. So that is why your subdomain redirects.
First you need to combine your rules because it seems you want to redirect if https is not on and there is no www, so make that one rule. Then use your actual domain name in the rule. Replace example.com with your domain. This should fix your issue.
#HTTPS Redirection
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com
RewriteRule ^(.*)$ https://www.example.com%{REQUEST_URI} [L,R=301]
Have an additional skip condition for all domains except the main domain:
RewriteCond %{HTTP_HOST} ^(www\.)?sta-games\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.sta-games.com%{REQUEST_URI} [L,R=301,NE]
Test this after clearing your browser cache.

htaccess force www not for cdn

I have a problem with .htaccess forcing www.
I have the main site http://www.portalesardegna.com and I want to redirect http://portalesardegna.com to the www version, but I have even a CDN, http://cdn.portalesardegna.com, if I look the site with pingdom http://tools.pingdom.com/fpt/#!/eHy16D/www.portalesardegna.com all the files in the cdn are redirected to the www version.
Some information that can be useful:
The CDN is on an external service, and the dns point to another server, not mine.
I have some images from another subdomain in my site, private3.portalesardegna.com, and all of these images are not redirected.
Here is the code i use in the htaccess
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^cdn\..+$ [NC]
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Try this rule as as your very first rule in your root .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(cdn|www)\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]