LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 07-13-2023, 01:17 AM   #1
1s440
Member
 
Registered: Mar 2018
Posts: 266

Rep: Reputation: Disabled
apache redirect to domain name


Hi all,

I would need to redirect webpage from IP(https://ip) to domain name(https://domain.com) instead. I placed the below redirect rule. unfortunately redirect the webpage from ip doesnot work for https but works with http. Can anyone suggest.

I also followed the below article.

https://www.ezeelogin.com/kb/article...linux-525.html
Code:
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^xx.xx.xx.xx$
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L,NE]

RewriteCond %{HTTP_HOST} ^xx.xx.xx.xx$
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L,NE]

Last edited by 1s440; 07-13-2023 at 03:27 AM.
 
Old 07-13-2023, 10:32 AM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,174
Blog Entries: 1

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
Quote:
Originally Posted by 1s440 View Post
Hi all,

I would need to redirect webpage from IP(https://ip) to domain name(https://domain.com) instead. I placed the below redirect rule. unfortunately redirect the webpage from ip doesnot work for https but works with http. Can anyone suggest.

I also followed the below article.

https://www.ezeelogin.com/kb/article...linux-525.html
Code:
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^xx.xx.xx.xx$
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L,NE]

RewriteCond %{HTTP_HOST} ^xx.xx.xx.xx$
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L,NE]
You don't need the initial check for http/https. Regardless of protocol you should only redirect the IP to the FQDN using https:
Code:
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^xx.xx.xx.xx$
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L,NE]
 
Old 07-13-2023, 10:57 AM   #3
thinknix
Member
 
Registered: Nov 2008
Distribution: Lots!
Posts: 178

Rep: Reputation: 58
And a minor addition to the solution, you may want to add in the 'QSA' flag so that query strings (anything after a '?', like https://xx.xx.xx.xx?foo=bar) are preserved. If you're not using query strings then you can disregard.

Code:
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^xx.xx.xx.xx$
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L,NE,QSA]
 
Old 07-14-2023, 04:01 AM   #4
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by bathory View Post
You don't need the initial check for http/https. Regardless of protocol you should only redirect the IP to the FQDN using https:
Code:
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^xx.xx.xx.xx$
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L,NE]
for somereason, it doesnot redirect now, the problem I have is certificate authenticates with domain name but not with ip. Hence i would like to redirect from ip to domain name
 
Old 07-14-2023, 07:37 AM   #5
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,174
Blog Entries: 1

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
Quote:
Originally Posted by 1s440 View Post
for somereason, it doesnot redirect now, the problem I have is certificate authenticates with domain name but not with ip. Hence i would like to redirect from ip to domain name
It redirects http://IP to https://domain.com, but not https://IP to https://domain.com, because the certificate is valid for domain.com.
This is how SSL works: A certificate is issued of one or more hosts and it's valid only for these. If you use the same certificate to access a different host, it will show a warning.

You should either issue a new certificate for IP, or accept permanently the certificate when prompted while visiting https://IP. After that the redirect will happen automatically...
 
Old 07-17-2023, 02:18 AM   #6
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by bathory View Post
It redirects http://IP to https://domain.com, but not https://IP to https://domain.com, because the certificate is valid for domain.com.
This is how SSL works: A certificate is issued of one or more hosts and it's valid only for these. If you use the same certificate to access a different host, it will show a warning.

You should either issue a new certificate for IP, or accept permanently the certificate when prompted while visiting https://IP. After that the redirect will happen automatically...
Got it

Code:
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^xx.xx.xx.xx$
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L,NE]
it redirects only http://ip to https://ip

Last edited by 1s440; 07-18-2023 at 04:00 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
apache redirect from new to old domain server, but browser visible old domain name hocheetiong Linux - Server 1 05-19-2013 04:28 PM
redirect https://www.domain.com to https://domain.com decenter Linux - Server 4 09-13-2011 10:05 AM
Set up .name.domain, but how do I set .domain/name Karas Linux - Newbie 1 12-03-2009 09:31 AM
redirect user.domain.com to www.domain.com/user macadam Linux - Newbie 2 05-15-2008 01:07 PM
what would make ever virtual domain name resolve to one domain name on my system kuplo Linux - Newbie 1 11-14-2005 06:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 01:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration