Google search results are showing my pages as (ip)/mypage.html instead of https://www.mydomain.com/mypage.html. I believe the solution is to redirect the ip's to the domain. I've found many, very similar ways to do this, but none of them are working for me. I have an existing rule that redirects http to https. This is what my .htaccess file currently looks like:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^11\.11\.11\.111$ [NC]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^([a-z.]+)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
What am I doing wrong?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^111\.111\.111\.111
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]
Alter "111" to your IP
Your 2 rewrite conditions clash. They require http_host to be 11.11.11.111 and to be *.mydomain.com, at the same time. Just add an or like so:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^11\.11\.11\.111$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
update to willian's answer. you just need to replace the domain name (your-domain.io) from this snippet.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$
RewriteRule (.*) https://your-domain.io/$1 [R=301,L]
Hi there we had requirements to do this as well due to a trigger index in the main pub_html folder. These rules should mask the IP to the http (or https if you switch em), make non-www into www. This should also preserve subdomains. This is for (1) simple site sitting in the pub_html, so test with care if you have subdomain triggers or whatever else in your htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ https://www.exampledomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^50\.28\.55\.76$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?exampledomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ http://www.exampledomain.com/$1 [R=301,L]
Hope it helps and works for ya'll. Thanks for the thoughts.
Related
Google search results are showing my pages as (ip)/mypage.html instead of https://www.mydomain.com/mypage.html. I believe the solution is to redirect the ip's to the domain. I've found many, very similar ways to do this, but none of them are working for me. I have an existing rule that redirects http to https. This is what my .htaccess file currently looks like:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^11\.11\.11\.111$ [NC]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^([a-z.]+)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
What am I doing wrong?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^111\.111\.111\.111
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]
Alter "111" to your IP
Your 2 rewrite conditions clash. They require http_host to be 11.11.11.111 and to be *.mydomain.com, at the same time. Just add an or like so:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^11\.11\.11\.111$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
update to willian's answer. you just need to replace the domain name (your-domain.io) from this snippet.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$
RewriteRule (.*) https://your-domain.io/$1 [R=301,L]
Hi there we had requirements to do this as well due to a trigger index in the main pub_html folder. These rules should mask the IP to the http (or https if you switch em), make non-www into www. This should also preserve subdomains. This is for (1) simple site sitting in the pub_html, so test with care if you have subdomain triggers or whatever else in your htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ https://www.exampledomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^50\.28\.55\.76$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?exampledomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ http://www.exampledomain.com/$1 [R=301,L]
Hope it helps and works for ya'll. Thanks for the thoughts.
I am trying to Canonicate an IP to redirect to the domain name, but for some reasons it won't work and constantly having https://www.111.111.111.11.
Here is what I am using in my .htaccess
RewriteEngine On
RewriteBase /
#Uncomment lines below when uploading to the live server
RewriteCond %{HTTP_HOST} ^111\.111\.111\.11$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.org$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} !^(www\.)example\.org$ [NC]
RewriteRule ^.*$ https://www.example.org[L,R=301]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https://(www\.)?example\.org/.*$ [NC]
RewriteRule \.(gif|jpg|png|tif|js|css|xls|xlsx|zip|rar|pdf|ods|ots)$ - [F]
Additionally, plesk takes care of a safe SEO redirect from example.org to www.example.org and also from http to https.
I am not very familiar with .htaccess rules, but even to me it looks like I have some unnecessary lines in there.
Can anyone please help?
Thanks in advance
In public directory, you must be sure that 000-default.conf file has AllowOverride All set, so the settings in your .htaccess file will be used.
Use these following in your .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^111\.22\.33\.44$
RewriteRule ^(.*)$ http://www.domainname.com/$1 [L,R=301]
I have these rules in my htaccess. one is for the main domain of the server and the others are other domains that are pointed at the server.
the purpose is to redirect subdomains to a url that is written a certain way. the main domain excludes certain subdomains from the redirect.
RewriteCond %{HTTP_HOST} !^(www|dev|mydev|something|somethingelse)\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-.]+)\.maindomain\.com [NC]
RewriteRule ^(.*)$ https://maindomain.com/index/parser.php?%2&domain_name=maindomain.com [QSA,L]
RewriteCond %{HTTP_HOST} ^(.*)\.anotherdomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-.]+)\.anotherdomain\.com [NC]
RewriteRule ^(.*)$ http://anotherdomain.com/index/parser.php?%2&domain_name=anotherdomain.com [QSA,L]
RewriteCond %{HTTP_HOST} ^(.*)\.whatever\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-.]+)\.whatever\.com [NC]
RewriteRule ^(.*)$ https://whatever.com/index/parser.php?%2&domain_name=whatever.com [QSA,L]
I don't want to keep having to add a new 3 lines for each new domain pointed at the server. How do I create one rule set to incorporate any domain that is pointed at the server? I would still need certain subdomains to be excluded from the main domain, but it wouldn't matter if the same subdomains are excluded from all the domains.
here are a couple things I tried. neither works.
#attempt 1
RewriteCond %{HTTP_HOST} ^(?:www\.|)([a-z0-9-]+)\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteRule ^(.*)$ https://%2/index/parser.php?%1&domain_name=%2 [QSA,L]
#attempt 2
RewriteCond %{HTTP_HOST} !^(www|dev|mydev|something|somethingelse)\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteRule ^(.*)$ https://%2/index/parser.php?%1&domain_name=%2 [QSA,L]
Not sure if I solved it, but this seems to be working. not sure if i can make it even better to handle http and https at the same time, and to handle many exceptions for main domain but just the one exception for the rest, but this is satisfactory enough for my needs. most domains will be ssl, so only need to edit htaccess files to add non ssl domains.
RewriteEngine On
#remove www from any domain pointed at box whether it's ssl or not
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]
#main domain with subdomain exceptions
RewriteCond %{HTTP_HOST} ^(.+)\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^dev\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^mydev\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^something\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^somethingelse\.maindomain\.com$ [NC]
RewriteRule ^ https://maindomain.com/index/parser.php?%1&domain_name=maindomain.com [L,R]
#non ssl domains here. three lines each.
RewriteCond %{HTTP_HOST} ^(.+)\.anotherdomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.anotherdomain\.com$ [NC]
RewriteRule ^ http://anotherdomain.com/index/parser.php?%1&domain_name=anotherdomain.com [L,R]
RewriteCond %{HTTP_HOST} ^(.+)\.whatever\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.whatever\.com$ [NC]
RewriteRule ^ http://whatever.com/index/parser.php?%1&domain_name=whatever.com [L,R]
#all ssl domains use this one rule.
RewriteCond %{HTTP_HOST} ^(.+)\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteCond %{HTTP_HOST} !^www\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteRule ^(.*)$ https://%2/index/parser.php?%1&domain_name=%2 [QSA,L]
My domain www.abc.com is now redirecting to https://www.abc.co.uk
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]
These code works exactly what I want. After that I created a subdomain. www.x.abc.co.uk
Now I want to redirect that to http://x.abc.co.uk (without https)
I used this
RewriteCond %{HTTP_HOST} ^x\.abc\.co\.uk [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But browser says it has redirect loop.
How can do that?
If these are two different htaccess files then give this a try.
RewriteEngine On
#redirect www.x.abc.co.uk without www to http
RewriteCond %{HTTP_HOST} ^www\.x\.abc\.co\.uk [NC]
RewriteRule ^(.*)$ http://x.abc.co.uk%{REQUEST_URI} [L,R=301]
I modified your original rule so you don't have two rewrite rules and still direct to www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*)$ https://www.abc.co.uk%{REQUEST_URI} [L,R=301]
I redirected non-www request to www through .htaccess Rewrite Rule.
RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*) www.%{HTTP_HOST}/$1 [L,R=301]
But now I am having problems with subdomains. When I am accessing touch.111.com then the above rule redirects to touch.www.111.com (which is not accessible), and the website breaks on touch devices.
Please advise me on how to fix the above problem.
You must be specific if you want to redirect only domain.com to www.domain.com and retain sub-domains (such as touch.domain.com) :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.*) http://www.domain.com/$1 [L,R=301]
This is a generic solution that can work for any domain name:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
see http://www.htaccessredirected.com/force-redirecting-non-www-to-www-htaccess.htm
# For sites running on a port other than 80
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R]
# And for a site running on port 80
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R]
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html