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?
city.php
is defined as theDirectoryIndex
then you currently have another file that is handling such requests?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..htaccess
file is used to make the site more "SEO-friendly". Instead of referring towww.1.example/city.php?q=blah_blah_blah
, just enterwww.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.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.