1

i need to rewrite URL from

us.example.com

to

example.com?country=us

without subdomains

I tried these 2 rules:

1.

RewriteRule (US|EG) ?c=$1 [NC , L]

2.

RewriteRule :\/\/(.*?).example.com ?C=$1 [NC , L]

but they didn't work.

so .. is there any way to do this?

MrWhite
  • 11,643
  • 4
  • 25
  • 40
Amr Ahmed
  • 13
  • 2
  • Do your subdomains point to the same area of the filesystem as the main domain? – MrWhite Jul 24 '19 at 22:02
  • ...And what do you want to do with the URL-path? Anything? What is the actual _file_ you are redirecting to? (`example.com?country=us` isn't strictly valid.) – MrWhite Jul 25 '19 at 08:20

1 Answers1

0

You can't "rewrite" a URL across different hostnames (you would need to implement a reverse proxy) and I assume a "redirect" is undesirable.

However, if the subdomains and main domain all point to the same area of the filesystem then you don't need to change the hostname/domain. A ordinary rewrite (on the same host) is all that's required.

To check the subdomain on the requested host, you need a condition (RewriteCond directive) that checks against the HTTP_HOST server variable (ie. the value of the Host HTTP request header). The RewriteRule pattern matches against the URL-path part of the URL only.

For example:

RewriteEngine On

RewriteCond %{QUERY_STRING} !^country=
RewriteCond %{HTTP_HOST} ^(us|eg)\.example\.com [NC]
RewriteRule ^ /?country=%1 [L]

As noted above, this assumes the subdomains and main domain point to the same area of the filesystem. This is common on a system where all requests are managed by a single CMS. (And you aren't making any attempt to change the hostname in your example directives.)

The %1 backreference holds the subdomain ("us" or "eg" in this example) from the requested hostname.

The additional condition that checks the query string for the absence of the country= URL parameter is to prevent a rewrite loop.

However, you've not stated anything about the URL-path? And you should be rewriting directly to the file that is handling the request (index.php?). In its current state, the above "rewrite" is reliant on mod_dir making an additional subrequest for the DirectoryIndex.


  1. RewriteRule (US|EG) ?c=$1 [NC , L]
  2. RewriteRule :\/\/(.*?).example.com ?C=$1 [NC , L]

There are numerous errors with these directives:

  • You say you've tried these, however, the erroneous spaces in the flags argument will result in an immediate 500 Internal Server Error when the file is first parsed.
  • As mentioned above, the RewriteRule pattern (first argument) matches against the URL-path only. This does not include the hostname.
  • The regex in #1 is far too general and matches "US" or "EG" anywhere.
  • In order to match against an absolute URL (as you appear to be trying to do in the second example) then you would need to match against THE_REQUEST server variable (which contains the first line of the request headers, include the request method).
MrWhite
  • 11,643
  • 4
  • 25
  • 40