Eamorr Eamorr - 1 month ago 17
Apache Configuration Question

Apache2 rewrite - conditional

I have the following

.htaccess
file in my project's web root:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /repos/eamorr.co.uk/react_www/index.html [L,QSA]


The above works.

When I navigate to:

http://localhost/repos/eamorr.co.uk/react_www

I see my homepage.

And when I navigate to:

http://localhost/repos/eamorr.co.uk/react_www/contact-us

I see the "contact-us" route of my homepage (my webpage is written in React).

OK. Great.

Now, some users are on Linux:

When they checkout the code, they see the site at a slightly different url:

http://localhost/_eamorr/develop/eamorr.co.uk/react_www/

and

http://localhost/_eamorr/develop/eamorr.co.uk/react_www/contact-us

Regarding this line of the
.htaccess
file:

RewriteRule . /repos/eamorr.co.uk/react_www/index.html [L,QSA]


Instead of
.
is there some conditional that I can put in to serve index.html from a different path?

e.g.

RewriteRule {{condition1???}} /repos/eamorr.co.uk/react_www/index.html [L,QSA]


or (different condition = different path):

RewriteRule {{condition1???}} /_eamorr/develop/eamorr.co.uk/react_www/index.html [L,QSA]


I'm afraid my Apache knowledge isn't up-to-speed to do this quickly and I've spent hours at this!

=================

Solution:

Use a cascading htaccess rewrite rule setup as follows:

RewriteEngine On
#RewriteBase /


#Linux localhost:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^.*repos/eamorr.co.uk/react_www.*$
RewriteRule ^([^/]+\.?[^/])*$ /repos/eamorr.co.uk/react_www/index.html [L,QSA]


#Mac localhost:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^.*_eamorr/eamorr.co.uk/react_www.*$
RewriteRule ^([^/]+\.?[^/])*$ /_eamorr/eamorr.co.uk/react_www/index.html [L,QSA]


#develop .co.uk
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^.*gitWebFlow/eamorr.co.uk/develop/react_www.*$
RewriteRule ^([^/]+\.?[^/])*$ /gitWebFlow/eamorr.co.uk/develop/react_www/index.html [L,QSA]


#master .co.uk
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^.*gitWebFlow/eamorr.co.uk/master/react_www.*$
RewriteRule ^([^/]+\.?[^/])*$ /gitWebFlow/eamorr.co.uk/master/react_www/index.html [L,QSA]


#default
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L,QSA]


This is good enough for my needs right now.

Answer

If react_www is your project's root, and you're not referencing assets above that directory (publicly, via URLs), that's where the .htaccess should be. Then it would become

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . index.html [END]

You shouldn't need RewriteBase since you're not doing server-side external redirects, and just want virtual URLs below that directory to all serve the same file.