How to centralize .htaccess rules in httpd.conf

Ludwig Diehl
Ludwig Diehl used Ask the Experts™
on
Hello everyone!

I was wondering how to set a rewrite rule on the httpd.conf rather than placing a .htaccess on every location

My directory structure:
https://mydomain.com/services/name/rest/index.php
https://mydomain.com/services/name/rest/.htaccess

How to invoke:
https://mydomain.com/services/name/rest/operation/value

My .htaccess content:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^((?s).*)$ index.php?_action=$1 [QSA,L]
</IfModule>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dr. KlahnPrincipal Software Engineer

Commented:
Just move that code into httpd.conf.

In general it is not necessary to conditionalize on whether mod_rewrite is available.  mod_rewrite is compiled in by default unless Apache was rebuilt specifically without it.
Ludwig DiehlSystems Architect

Author

Commented:
it is not that simple because I only want to apply that rule on a certain condition.

So the condition should be something like this:

https://mydomain.com/services/*/rest/*

Only when /services/*/rest matches
David FavorLinux/LXD/WordPress/Hosting Savant
Distinguished Expert 2018

Commented:
If I understand what you're asking, take a look at how Ubuntu (and all Debian derivatives) handle this.

They provide a directory called /etc/apache2/sites-available for virtual hosts.

Then for other common directives, like logging, create a logging.conf file in /etc/apache2 + include it in every virtual host setup file.
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Ludwig DiehlSystems Architect

Author

Commented:
Of course I understand what I am asking. I yeah, I like the way debian and Suse handle this. But it is just a matter of creating such a folder and placing there all my vhosts conf files separately. I was just wondering how to achieve what I asked for using wildcards.
Linux/LXD/WordPress/Hosting Savant
Distinguished Expert 2018
Commented:
Expand a bit on your use of the word wildcards.

Normally, in the Ubuntu model, you'll have a file for bare domains + then another file for each separate host, which allows hosts to be added/deleted/changed easily.

If by wildcard you mean, adding many hosts to one .conf file, I personally prefer separate files, as I work with 100s of config files inside 100s of LXD containers, so separate files allows easier maintenance.

And, there's no requirement for this.

Once you a2ensite file.conf then all domains + hosts in file.conf become active, so you can create a single file with 100s or 1000s of domain + host entries, if you like.
Ludwig DiehlSystems Architect

Author

Commented:
Thank you everyone. In the end I had to create separate files.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial