Apache .htaccess load subdirectory only if it exists - apache

My current directory structure is /www/<project> which maps to localhost/<project>. What I would like to do is load localhost/<project> with /www/<project>/dist ONLY if a dist directory exists in the project directory. How can I do this with within .htaccess assuming rewrite is turned on?

You can use RewriteCond to test if a file or directory exists.
You can perform various file attribute tests:
'-d' (is directory)
Treats the TestString as a pathname and tests whether or not it exists, and is a directory.
'-f' (is regular file)
Treats the TestString as a pathname and tests whether or not it exists, and is a regular file.

Related

where to put a file to be exec'ed by an apache mod_xxx?

I'm trying to exec a file from an apache mod_xxx on a ubuntu system. The exec fails (return status -1). A test program outside of the apache mod forks and exec's the file correctly.
Some investigation (trying to create a /tmp/xxx file) revealed that the created file, despite having an absolute path, was in a subdirectory, e.g.
/tmp/systemd-private-402d6822d1a14c4c8da5c6fb450b85ea-apache2.service-YsvK3S/tmp/apache_mod_locator
My question is, where should I put a file to be exec'd by an apache mod_xxx, and what pathname should I use to refer to it? I've tried an absolute path, and the file-name alone when it resides in the same place as the mod_xxx.so.
At the moment the file resides in my personal file hierarchy, not a "standard" system location like /usr/bin.

Serving express files from base directory

How do would I serve static files from my base directory? Would it just be a / or would I have to include the name of the base directory, which in this case would be Scanning
app.use(express.static(join(__dirname, '/')));
Pretty close, just a few adjustments!
app.use(express.static(`${__dirname}/Scanning`))
You need to use or construct the actual full path to the base directory. You don't show your actual directory structure and where the desired directory is relative to the directory that your code is running from.
If you wanted express.static() to serve from the Scanning directory which is a sub-directory of the directory you code is located in, you would do this:
app.use(express.static(path.join(__dirname, 'Scanning')));
Or, if Scanning is a sibling of __dirname, then it would be this:
app.use(express.static(path.join(__dirname, '../Scanning')));
You should never be serving files with express.static() directly from __dirname because that would allow your server to serve up your actual source files (and sometimes things like credentials).

Apache server cannot find local file [duplicate]

I'm working on a Flask extension from which I want to create a directory in the project's root path on the file system.
Suppose we have this directory structure
/project
/app
/tests
/my_folder
manage.py
my_folder should be created dynamically by the extension, which is a test utility and wraps the application under test in the /tests directory. However, I'm struggling to determine the project's root path within my extension.
For now, I am trying to guess the path from the run file:
def root_path(self):
# Infer the root path from the run file in the project root (e.g. manage.py)
fn = getattr(sys.modules['__main__'], '__file__')
root_path = os.path.abspath(os.path.dirname(fn))
return root_path
This obviously breaks as soon as the tests are run from within the IDE instead of the manage.py. I could simply infer the project's root relative to the app or tests directory, but I don't want to make any assumptions regarding the name or structure of these directories (since multiple apps might be hosted as subpackages in a single package).
I was wondering if there is a best practice for this type of problem or an undocumented method which the Flask object provides (such as get_root_path).
app.root_path contains the root path for the application. This is determined based on the name passed to Flask. Typically, you should use the instance path (app.instance_path) not the root path, as the instance path will not be within the package code.
filename = os.path.join(app.instance_path, 'my_folder', 'my_file.txt')
app.root_path is the absolute path to the root directory containing your app code.
app.instance_path is the absolute path to the instance folder. os.path.dirname(app.instance_path) is the directory above the instance folder. During development, this is next to or the same as the root path, depending on your project layout.

rsync backs up everything

I am trying to backup some of the essential folders in the / in my ubuntu system. I am using
sudo rsync -aAXv --delete --include="/etc" --include="/home" --include="/usr/local" // /home/$USER/Desktop/bkup/
This command should only copy /etc, /home, /usr/local dirs and leave the rest of the files. But, when I run this command this copies every dir and every file in the / dir.
I am not sure what wrong I am doing here.
Includes without any excludes are meaningless.
--exclude='*' would exclude everything not explicitly included, from every subfolder, even the included ones.
--exclude='*/' would exclude every directory not explicitly included, but allow copying files within included directories (and the root).
--exclude='/*' would exclude all root directories and files not explicitly included, but allow all directories and files within included directories. You probably want this one.
You should add your exclude rule after your include rules. The rule is that, for each directory and file, it's the first matching include/exclude rule that matters, and the default (when no rule matches) is to include.
By "root" I mean the root of the copied directory, not the root of the whole file system.
P.S. Your command also has the destination directory inside the source directory; you probably want an exclude rule for that!

Is Apache wildcard Include directive recursive?

According to Apache manual, the Include directive is recursive when a directory path is used. But is it recursive when using a wildcard path?
Include "/usr/local/apache/conf/userdata/std/2/username/domain.com/*.conf"
I checked it and it is not recursive.
As Joyce already said, I can confirm by testing it myself that it is not recursive.
Include uses fnmatch as wildcard engine, which doesn't match a slash by default, unless the FNM_PATHNAME flag is set, so a * doesn't match / so domain.com/*.conf will not look in sub-directories.
However, since httpd 2.3.6 it is possible to also use the wildcard for sub-directories.
Examples
Include /usr/local/apache2/conf.d/ssl.conf
This matches only a specific file.
Include /usr/local/apache2/conf.d
If conf.d is a file, it matches only this file. If conf.d is a directory, all files will be matched recursively, including files in sub-directories and non-conf files (which causes an error).
Include /usr/local/apache2/conf.d/*.conf
This will only match the files with a .conf suffix, directly located in the conf.d directory. Files in sub-directories aren't matches.
Include /usr/local/apache2/conf.d/*/*.conf
This will only match the files with a .conf suffix, directly located in sub-directories of the conf.d directory, but it will NOT match files directly located in the conf.d directory.
So for example, if you need to match all .conf files directly located in conf.d and in first level of sub-directories and second level of sub-directories, you can use this:
Include /usr/local/apache2/conf.d/*.conf
Include /usr/local/apache2/conf.d/*/*.conf
Include /usr/local/apache2/conf.d/*/*/*.conf
If you only have valid configuration files in conf.d and want to match every level of subdirectories, then you can use:
Include /usr/local/apache2/conf.d
Instead of using wildcard, you should use a directory.
It has been supported since as early as 1.3 https://httpd.apache.org/docs/1.3/mod/core.html#include
New in Apache 1.3.13 is the feature that if Include points to a directory, rather than a file, Apache will read all files in that directory, and any subdirectory, and parse those as configuration files.