DevHeads.net

mod_rewrite question on directory

We have a test server, with test sites that are in two different branches
of development, but essentially the same base content. They live at
/x/y/z/testsite and /x/y/z/test-site. We have other sites such as wikis
and one-offs which need to stay online on our production server, and I have
been testing using rewrites to force the URL to conform to the directory
path that is defined in the .conf file.

So for example, in the testsite.conf file, I have the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^/$ <a href="https://testsite.example.com/testsite/" title="https://testsite.example.com/testsite/">https://testsite.example.com/testsite/</a> [L,R]

alias /testsite "/x/y/z/testsite"
<Directory "/x/y/z/testsite">
Require all granted
RewriteEngine On
RewriteBase /testsite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
RewriteCond %{HTTPS} Off
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} !testsite.example.com
RewriteRule ^.*$ https://testsite.example.com%{REQUEST_URI} [R]
</Directory>

The test-site.conf file is the exact same except for adding the hypen in
the names.

We also have the following in ssl.conf for these:
RewriteRule ^(.*)/testsite$ $1/testsite/ [R,NC]
RewriteCond %{HTTP_HOST} testsite\.example\.com
RewriteCond %{REQUEST_URI} ^/testsite(.*)
RewriteRule (.*) https://testsite.example.com%{REQUEST_URI}
[R]
RewriteCond %{HTTP_HOST} testsite.example.com
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) <a href="https://testsite.example.com/testsite/" title="https://testsite.example.com/testsite/">https://testsite.example.com/testsite/</a> [R]

RewriteRule ^(.*)/test-site$ $1/test-site/ [R,NC]
RewriteCond %{HTTP_HOST} test-site\.example\.com
RewriteCond %{REQUEST_URI} ^/test-site(.*)
RewriteRule (.*) https://test-site.example.com%{REQUEST_URI}
[R]
RewriteCond %{HTTP_HOST} test-site.example.com
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) <a href="https://test-site.example.com/test-site" title="https://test-site.example.com/test-site">https://test-site.example.com/test-site</a> [R]

If I leave this as shown, and restart the service, neither page loads at
all. If I comment out the three lines after "RewriteRule ^(.*)/testsite$"
and "RewriteRule ^(.*)/test-site$" respectively, the sites load properly.
We have this exact set of rewrites on ssl.conf for all sites on the
production server and it rewrites the URL properly. So I'm not sure where
it's failing on the test site. Logs are set to trace8 on the test server
and I'm not getting anything that helps tells me where the problem is.

jim

Comments

Re: mod_rewrite question on directory

By Igor Cicimov at 06/11/2019 - 01:33

Since you already have two separate domains why not use virtual hosts each
with it's own document root?

IC

Re: mod_rewrite question on directory

By Frank Gingras at 06/11/2019 - 08:18

You are also grossly abusing mod_rewrite for this. It isn't needed at all.

Use FallbackResource, Redirect, and separate vhosts, as Igor mentioned.

On 11/06/19 01:33 AM, Igor Cicimov wrote:

Re: mod_rewrite question on directory

By Jim Weill at 06/11/2019 - 13:24

The sites I am trying to model are drupal-based. We aren't dealing with
plain static HTML or PHP sites. We have the main server, which this test
server is trying to mirror, and a secondary project server which exists to
give project people limited root access to update their own code. The
secondary server also has drupal-based sites, but the redirections I
describe in the issue work on the secondary server. They were never
implemented in any way on the main production server. If I put
wiki2.example.com, it should return wiki2.example.com/wiki2. And so if I
have site1, wiki2, and www on the main server, I can go to
wiki2.example.com/www and get the main www site, when we want the main site
to always turn up <a href="http://www.example.com/www" title="www.example.com/www">www.example.com/www</a>.

Is this not what mod_rewrite is meant to solve? I fully admit I am not
well-versed in apache, my prior work was mainly Windows Server and
Exchange, but I am trying to learn this stuff based on what we had set up
long before I came here.

jim

Re: mod_rewrite question on directory

By Frank Gingras at 06/11/2019 - 16:30

No, I assure you, ,mod_rewrite is not needed here.

To enforce a canonical hostname, use the Redirect directive and separate
vhost. To have all requests handled by a php routing script, use
FallbackResource /path/to/file.php

Lastly, to redirect to https://, use Redirect from a :80 vhost.

On 11/06/19 01:24 PM, Jim Weill wrote:

Re: mod_rewrite question on directory

By Igor Cicimov at 06/11/2019 - 17:33

Agree, much better than all that rewrite gymnastics.

IC

Re: mod_rewrite question on directory

By Jim Weill at 06/12/2019 - 17:42

So just to make sure I have this correct: the main production server has a
couple pages of rewrites for old URLs and home pages that moved servers or
changed path. And changing the baseURL with a Redirect directive in a
vhost instead of mod_rewrite will not break any of that?

jim

Re: mod_rewrite question on directory

By Frank Gingras at 06/13/2019 - 08:02

Jim,

Using mod_alias or mod_rewrite with [R] is functionally equivalent, yes.
The former is simpler and easier to maintain, of course.

Note that you can use RedirectMatch if you need PCRE support and/or
captured expressions.

On 12/06/19 05:42 PM, Jim Weill wrote: