2

I am trying to redirect all traffic to my site from a country to a sub-domain. I am using the following method to redirect the traffic.

RewriteEngine on
SetEnvIf CF-IPCountry "(.*)$" Country=$1
RewriteCond %{ENV:Country} ID
RewriteRule ^(.*)$ https://us.example.com/$1 [R,L]

ID is the country code, using Cloudflare IP Geolocation for the same.

Now coming to my issue. Using the above method only the homepage is being redirected, sub-pages of my main domain are not redirected. I came across a couple of methods where you have manually enter the details of each page. Is there a way to redirect all sub-pages to the sub-domain.

Need the below functionality,

https://example.com/page-1/ ---------> https://us.example.com/page-1/
https://example.com/page-2/ ---------> https://us.example.com/page-2/
3
  • The directives you posted should already "redirect all sub-pages". Do you have any other directives in your .htaccess file?
    – MrWhite
    Aug 9, 2018 at 11:01
  • @MrWhite Yes, pasting them below. # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> SetEnvIf CF-IPCountry "(.*)$" Country=$1 RewriteCond %{ENV:Country} US RewriteRule ^(.*)$ us.example.com/$1 [R,L] # END WordPress
    – Jijo Jose
    Aug 9, 2018 at 13:46
  • Not able to format the above comment properly. I am new here, so sorry.
    – Jijo Jose
    Aug 9, 2018 at 13:47

1 Answer 1

1

pasting them below. # BEGIN WordPress ...

That's the problem. These directives must go before the WordPress front-controller, otherwise they are simply never going to be processed for any requests other than the document root (homepage) or requests that map to physical files. All other requests will be routed through WP.

# Your directives go here...

# BEGIN WordPress
<IfModule mod_rewrite.c>
:

This is a surprisingly common pitfall. See this recent related question on the WordPress Stack: https://wordpress.stackexchange.com/questions/310761/redirect-to-https-not-working-with-htacess

1
  • 1
    That worked. Not able to vote as I am new. Thanks a lot for the help, you made a lot of people happy. :)
    – Jijo Jose
    Aug 9, 2018 at 15:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .