Redirecting a Domain without Changing URL - .htaccess

I am trying to redirect a domain to point to one of my other domains without changing the URL. Whenever I save the code I am running into a 500 internal server error. I am currently using the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain2.com
RewriteRule ^(.*) http://www.domain1.com/ [P]
RewriteCond %{SERVER_PORT} !443
RewriteCond %{REQUEST_URI} !^/.well-known
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I am trying to have domain2 point to domain1.
I have no experience in .htaccess, am I making a common mistake with this code?
Thanks

Related

Domain too many redirection

Hi after 1 week of research i'm still stuck with my domain redirections
I want my http://domain.fr pointing to https://www.domain.fr
I'm trying with htaccess file with no success...
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.*
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
When i'm testing my redirections it seems https://www.domain.fr is redirecting itself without understading why...
have you guys any idea ?
The problem is in your 2nd RewriteRule
RewriteCond %{HTTP_HOST} ^www\.*
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
You rule does a permanent redirection of www domain to the same location ie : ( www.e ample.com => www.example.com ) that is why you got the redirect loop error.
To redirect http urls to https ,first of all you need to check if the request scheme is http. You can use RewriteCond %{HTTPS} off to check the non - secure http connection and then use RewriteRule to redirect the request to https .
Use the following simple Rule to redirect http to https :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
Make sure to clear your browser cache before testing this.

htaccess redirect for www to non-www not working

This is driving me bananas. For some reason my domain will not work to redirect https-WWW to https-non-WWW. This works with every other permutation except https://www.nextoronto.com
What code do I use in the .htaccess that will allow it to work for all permutations?
Edit: This is the rewrite code now:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.nextoronto\.com [NC]
RewriteRule ^(.*)$ https://nextoronto.com/$1 [L,R=301]
It looks sound, perhaps that rule isn't being reached due to other rules in front of it?
Here is what I use that works:
# Force SSL
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://nextoronto.com/$1 [R=301,L]
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://nextoronto.com/$1 [R=301,L]
# My other rewrites for routing all requests to index.php go here...
Edit the URL and try with this code:
RewriteEngine on
rewritecond %{http_host} ^www\.example\.com [nc]
rewriteCond %{SERVER_PORT} 80
rewriterule ^(.*)$ https://example.com/$1 [r=301,nc]
Step 1
You should make .htaccess file like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1
Step 2
Go to the directory, then view mark the option file extension. Then see the .htaccess file.
When no rewrite works, no matter what you do, consider what happened to me. IIS or WAMP, on my laptop, redirected all calls to my domain (call it example.com) to localhost. So creating all the rewrites on my example.com server were to no avail because no calls to example.com ever made it out of my computer. Just do a "ping example.com" and if you get an IP of 127.0.0.1, edit your HOSTS file.

Issue in htaccess redirecting from http to https

I went the entire website to be accesed only with https.
In .htaccess to i written the following rule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.my-website.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.my-website.com/$1 [R,L]
But it causes "This webpage has a redirect loop"
Anyone can guide me a fix for this.
Normally with a single site, you can use that:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.my-website.com%{REQUEST_URI} [R=301,L]

Strip www. from domain works on main site but not with subdomain

I currently have my .htaccess file as the following:
ErrorDocument 404 /404.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
when I visit my primary site at: www.site.com, the www. is stripped as it should be and all works fine.
When I visit a subdomain in proper format (http://sub.site.com) everythign works fine.
The issue comes when a user would enter www. as part of a subdomain so:
http://www.sub.site.com does not strip the www. and instead redirects to the Media temple hosted "sub-domain does not exist" page (INCREDIBLY ANNOYING by the way).
Any suggestions?
Change these lines:
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
To:
RewriteCond %{HTTP_HOST} ^www\.(.+?)\.([^.]+)$ [NC]
RewriteRule ^(.*)$ http://%1.%2$1 [R=301,NC,L]
I believe I had to "trick" the system but found a working method:
I first had to create a vhost.conf file within my subdomain with:
ServerAlias www.subdomain.site.com
After resetting Plesk and my Apache server, I also added a wildcard DNS record to my subdomain as:
*.subdomain.site.com. A record with IP address appropriately
This seems to have resolved my issue for now though still can't figure out why the former did not work.

Mod_rewrite: How do I omit subdomains from rules?

Complete Apache newbie here. I'm trying to get my main URL to redirect to the www. Here's the code I'm using:
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
The problem is, this has wrecked my subdomains. sub.domain.com goes to www.sub.domain.com, which doesn't work. So what do I write to fix that?
This might be enough:
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
If all you're trying to do is make domain.com go to www.domain.com, then just use a RewriteCond that only matches domain.com at the start.

Resources