1

I use Apache web-server and I want to redirect all urls from ip to domain. Here is the content of my website .htaccess file:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

Options -Indexes

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([\s\S]*)$ index.php?rt=$1 [L,B,QSA]

According to some research, I guess I need something like this: (not sure)

RewriteBase /
RewriteCond %{HTTP_HOST} ^95\.216\.161\.63$
RewriteRule ^([\s\S]*)$ https://lamtakam.com/$1 [L,R=301]

But I don't know how (in which part) should I add those threelines to the .htaccess file. Any clue?

Martin AJ
  • 6,261
  • 8
  • 53
  • 111

1 Answers1

2

You should insert IP redirect rule before or after https/www redirect rule:

Options -Indexes
RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{HTTP_HOST} ^95\.216\.161\.63$
RewriteRule ^ https://lamtakam.com%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ index.php?rt=$1 [L,B,QSA]

If you want to make your IP matching condition more generic then use:

RewriteCond %{HTTP_HOST} ^\d+\.\d+\.

to be able to match any IP address.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    I see, very good. Just a short question, why did you replaced `^([\s\S]*)$` with `.*`? .. You know, my website is in Persian, some URLs will be broken with `.*`, but `[\s\S]` matches everything, that's why I used it – Martin AJ Oct 03 '19 at 13:58
  • Are you sure it gets broken by `.*`? I have tested non-English URLs in the past and everything was matched with `.*` because Apache encodes all UTF characters before we process them in `mod_rewrite` so all the whitespaces come as encoded. May be `B` can also be removed to make `.*` work. Please provide me an example URL that is broken with `.*` and I will surely learn something new. – anubhava Oct 03 '19 at 14:05
  • [here](https://stackoverflow.com/questions/34564364/how-to-pass-a-persian-string-as-a-argument-in-the-url) is the problem we were involved with for about 1 week. – Martin AJ Oct 03 '19 at 14:10
  • 1
    ok thanks, I will test it today itself. Meanwhile please keep using `[\s\S]*` – anubhava Oct 03 '19 at 14:12
  • Thanks. Also as my last question, do you think can I replace `.+` with `\s\S+` in `RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]` too? – Martin AJ Oct 03 '19 at 14:17
  • 1
    No that is not needed AFAIK because domain name can't allow all the characters that are allowed in REQUEST_URI – anubhava Oct 03 '19 at 14:18
  • 1
    I've tried it and worked well, thx again. Just out of curiosity, no need to use `RewriteBase /` ? – Martin AJ Oct 03 '19 at 18:30
  • 1
    Yes `RewriteBase /` is just a syntactic sugar here. It is used to prefix any relative targets and for non redirect rules (like last one) by default current path is already prefixed by `mod_rewrite` engine. – anubhava Oct 03 '19 at 18:33