2

I run the website https://www.1.example. The contents of my .htaccess file are as follows:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ \city.php?q=$1 [QSA,L]

I would like to connect the second domain https://2.example to this site. How to modify the .htaccess file so that it redirects the query to the appropriate file due the domain used by the visitor. My point is that when someone enters my website from the domain https://1.example or https://www.1.example, he should be redirected to the file city.php?q= and when he enters from the domain https://2.example or https://www.2.example to the file file2.php?q=.

5
  • What do you want to happen when requesting the document root (homepage)? Unless city.php is defined as the DirectoryIndex then you currently have another file that is handling such requests?
    – MrWhite
    Sep 18 at 20:51
  • As @MrWhite alludes, while this can be done, why would you want to use city.php rather than index.php which is usually the default file that is opened?
    – Steve
    Sep 18 at 21:47
  • @Steve (/Bartosz) It's perfectly reasonable to use a filename other than index.php for this, particularly in this case when there are two files (two "front-controllers"). I'm simply querying what is expected/currently happening for root requests, which is not clear from the question - the current directives do not handle this case (and I assume the current config is working as expected for one domain). Without more information then we would need to "guess" what the intention is.
    – MrWhite
    Sep 19 at 10:47
  • OK, I already explain everything. The redirection in the .htaccess file is used to make the site more "SEO-friendly". Instead of referring to www.1.example/city.php?q=blah_blah_blah, just enter www.1.example/blah_blah_blah I am going to launch a second site that will be very similar in content to the first site but will have a slightly different function. This second site, www.2.example, is to use the same resources as the first site, www.1.example, and is to be in the same folder on the server's disk.
    – Bartosz
    Sep 19 at 12:41
  • "I already explain everything." - Well, actually you've not. You've not addressed the question in my first comment... how are requests for the root directory (ie. the homepage) currently handled? The directives you've posted in the question do not handle such a request, so either you have other directives or another file (eg. index.php) that handles this request? This is relevant as it determines how the directives should be written and how requests for the root directory of the second domain should be handled.
    – MrWhite
    Sep 19 at 23:55

1 Answer 1

1

You've not stated how you are currently handling requests for the root directory (homepage), as this is not handled by the directives you have posted (since the second condition explicitly excludes all directories). However, I'll assume that all requests for the first domain are being handled correctly by the existing rule (and server config) and any requests for the second domain (including the root directory) should be sent to file2.php.

A couple of asides...

RewriteRule ^(.*)$ \city.php?q=$1 [QSA,L]

The backslash-escape at the start of the substitution string looks like an error/typo. There is no need to escape a literal c here. You probably meant to use a (forward) slash (ie. /), but this is not required either (and best omitted).

Minor point, but the start-of-string (^) and end-of-string ($) anchors on the RewriteRule pattern ^(.*)$ are not required since regex is greedy by default.

RewriteCond %{REQUEST_FILENAME} !-d

Do you need to make an exception for requests that map to physical directories? Do you need to be able to access directories themselves? Although you see this a lot, it's actually very rare that this would be required. Without this, your rules can be simpler and more efficient.

The presence of this condition will often mean that a request for a directory would result in a "403 Forbidden". Whereas, without this condition the request would be routed through your front-controller, which would probably result in a "404 Not Found" (which is often preferable).

Solution

Try the following:

RewriteEngine on

# (Optional) Abort early if either front-controller already reached
RewriteRule ^(city|file2)\.php$ - [L]

# If the request matches the first domain then skip the next 2 rules
RewriteCond %{HTTP_HOST} ^(www\.)?1\.example [NC]
RewriteRule ^ - [S=2]

# Second domain
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) file2.php?q=$1 [QSA,L]

# Second domain root directory (homepage)
RewriteRule ^$ file2.php [L]

# First domain
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) city.php?q=$1 [QSA,L]

Note that I also changed the RewriteRule pattern from .* to .+ which avoids an unnecessary filesystem check for requests to the document root.

There is also an additional question to ponder... what do you want to happen if the front-controller (ie. city.php or file2.php) is requested directly? Allow it? Block it? Redirect it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.