Conditional settings for a specific domain in .htaccess? - apache

I would like to have a conditional setting for a specific domian in my .htaccess file. My use case is I have dev (example.dev) and live (example.com). The htaccess file is in my git repo but I have some settings in .htaccess that should only apply to the live site. Ideally I would like to have a conditional setting like the pseudo code below:
<sometag domain=my_prod_domain example.com>
#code specific to only the live domain here
</sometag>

Do you have access to the main configuration files?
If yes, then you could specify a different configuration file for your different hosts using
AccessFileName .public.htaccess
and
AccessFileName .dev.htaccess
And then you could work with the Include directive to load configurations common to both.
Include .common.htaccess
Another idea would be to try to work with the If-Directives, but it all really depends on your setup and level of access to the configurations: http://httpd.apache.org/docs/2.4/mod/core.html#if
Edit:
Okay I think with IfDefine you can do something even more along the lines with you original though:
Start your dev. Server with httpd -DDevServer and have on your statements like this:
<IfDefine DevServer>
#Config specific for your dev. server
</IfDefine>
<IfDefine !DevServer>
#Config specific for your live server.
<IfDefine>
For this solution you do not need even root access on your live-server.

Related

Use alternative to .htaccess file if it exists?

Background: I used to use an environment variable to enable me to differentiate between my dev and production server, using this in .htaccess: -
<IfDefine DEV_SERVER>
# dev server specific stuff here
</IfDefine>
<IfDefine !DEV_SERVER>
# production server specific stuff here
</IfDefine>
The environment variable "DEV_SERVER" was only ever set on my dev server, and never on production, obviously.
This approach worked a treat until my production server host installed Litespeed as a replacement for Apache. Litespeed doesn't like the above, and ignores everything.
I tried having different versions of .htaccess files for dev and production, but that's messy and dangerous, I like to be able to interchange all files at any time between the two.
My dev server runs Apache 2.4 and I have no plans to change this. I read that you can specify a different filename for .htaccess by putting this in httpd.conf: -
AccessFileName .htaccess.dev
Which is half way there - now I can have a .htaccess.dev file which only my dev server will use in place of .htaccess. But ideally I would like the dev server to: -
use .htaccess if that's all there is
use .htaccess.dev if that's all there is
use .htaccess.dev if there are both .htaccess and .htaccess.dev present
It does (2) and (3), but unfortunately not (1).
Is there any solution? Any help greatly appreciated, as always!
You can specify multiple files in AccessFileName so you can do: AccessFileName .htaccess.dev .htaccess
What this will do is to prefer .htaccess.dev over .htaccess if it exists, and will use .htaccess if .htaccess.dev doesn't exist.
However, in my opinion you're doing something wrong, if you have to customize your .htaccess per environment :)
Update with working example:
If you put AccessFileName .htaccess.dev .htaccess in your Apache configuration (Such as the vhost it will work as it should):
# curl -sI http://example.com/test.html | grep "x-htaccess"
x-htaccess: dev
# mv .htaccess.dev htaccess.dev
# curl -sI http://example.com/test.html | grep "x-htaccess"
x-htaccess: default
The content of .htaccess is Header set x-htaccess default and the content of .htaccess.dev is Header set x-htaccess dev.

Run cakephp app in apache alias

I'm trying to deploy a cakephp 3.1 app in a apache 2.4 powered server. My boss would like to place the app in a directory different of server's documentroot (DocumentRoot "/var/www/html" in my httpd.conf file), since multiple webapps will be served by this server.
Instead of virtualhosts, he would like to use aliases (host/app1, host/app2 etc). So I'm trying to configure it this way. I put an alias to my cake app (Alias "/scqa" "/opt/scqa/webroot" in my httpd.conf file) and wrote a RewriteBase (RewriteBase /scqa) to both cake's .htaccess files, but every absolute link present in my app is still pointing to apache's documentroot. In this particular case, it means my css and a big pile of not properly built links are 404ing. Is there some way I can fix it in apache configuration?
I know 2 other ways to fix it: Fix the links with cake syntax (will take me a week) or use virtualhosts. But is it possible to fix it and keep using apache alias?
Thanks in advance.
(The production env uses centos 7 64bits, just in case)
If I understood well, you are using aliases in your .htaccess files.
You should put the Alias in your server config file, because it won't work otherwise.
Syntax: Alias [URL-path] file-path|directory-path
Context: server config, virtual host, directory
From https://httpd.apache.org/docs/current/mod/mod_alias.html

Apache server directory browsing while there is a website running

Is it possible to browse a directory on an Apache server with a running website?
Example: I have myserver/mydirectory with an index.html and 'test.txt`. Can I list somehow those files assuming browsing is enabled?
there are a couple of things you can try:
in httpd.conf find the line that begins with "DirectoryIndex" and replace it with "DirectoryIndex disabled" this way apache will not server default files like index.html and just list files. however you can explicitly request it if you want.
if default document setting is important to you, you can also configure apache to listen to another port and setup a virtual host on that port and do the same thing with "DirectoryIndex" for virtual host, this way you have two ports , one that serves default documents and one that only list files.
if you want to use only one port for this , you can try no. 2 option and then set a proxy that sends all requests that begin for example with /list/ to the other virtual host, this way you work on one port and if you want list of files instead of writing "/myserver/mydirectory" you request "/list/myserver/mydirectory"
hope it helps.
The DirectoryIndex directive in the Apache configuration tells Apache which index files to look for. Default settings includes index.html, so if you have such a file in your directory, this is the file that Apache will serve if you enter the site without specifying a specific file (this you properly already know, but included for completeness).
To enable directory listing in Apache, have a look at the Options Indexes option. For example in your case (assuming your website is located in /var/www/website:
<Directory /var/www/website/mydirectory>
Options Indexes FollowSymLinks
</Directory>
This will, however, only enable listing of files if Apache do not find an index file. A solution is therefore either to delete (or rename index.html), or to use a website scripting language like PHP to enable directory listing (For this, Google is your friend :-)

PHP: How to code .htaccess to make it work both on localhost & online without editing

I have a .htaccess file & I currently I am working on localhost. For a 404 page error, I have the following code in the .htaccess file:
ErrorDocument 404 /my_local_domain/404.php
But when I upload this file to my website online, the functionality of the file breaks. It no longer shows the 404.php page. It works if I modify the code in the .htaccess file of my online website to the following:
ErrorDocument 404 /404.php
Now all through the changes that I do in the .htaccess file, I would have to remember to remove the domain name before I upload it to the website or I risk breaking the functionality. So with this in mind, here are my questions:
1. How do I solve the above problem without needing to edit the .htaccess file each time (by stripping it off the my_local_domain) I make a change & upload it online?
2. How do I setup 404 page redirection for all the nested folders? (I don't want to setup a .htaccess file for each of the folders. A single .htaccess file that resides in the root folder of the website & controls all the redirection for all the sub-folders would be awesome)
All help is appreciated.
Thank you.
I believe you have two different issues here.
First of all, you should not need to have different paths in development and live site. It appears that you've configured your local Apache to host only one site and each actual sites goes in a subdirectory. It's not a good idea: you'll soon be mixing cookies and sessions between all your dev sites. Have a look at the name based virtual hosts feature: you can configure as many independent sites as you need. You don't even have to buy real domains in you set them in the hosts file.
Secondly, under certain circumstances it can be useful to have different Apache directives. I've been using the following trick.
Pick a keyword for the dev server, e.g. DEV_BOX.
Pass that keyword to Apache in the -D parameter. If you run it as service, you can run regedit and find the HKLM\SYSTEM\CurrentControlSet\Services\Apache2.2\Parameters key. Append -D DEV_BOX to the ConfigArgs value. Restart Apache.
Now, you can use the <IfDefine> directive to set local directives:
-
#
# Common stuff
#
AddDefaultCharset UTF-8
#
# Local-only stuff
#
<IfDefine DEV_BOX>
Options +Indexes
</IfDefine>
#
# Live-only stuff
#
<IfDefine !DEV_BOX>
Options -Indexes
</IfDefine>
First of all I suggest you setup local domains for development. For example if you are developing a website which will go under www.example.com, you can setup a local.example.com in your HOSTS file. You'll do a VirtualHost setup in your apache and the .htaccess will then be the same.
Also, you can setup a build process (e.g via Ant) which will allow you to prepare and generate a zip file with the files which go on the live server. This build will feature the correct configuration files (db configs, mail servers, htaccess etc).

How can I get apache to list a folders contents?

How can I get Apache to display the contents of my folder and provide links to them? Similar to http://www.kernel.org/pub/linux/?
I don't have access to the Apache configuration, so I'm looking for something in the way of .htaccess or something I can do in just my home folder.
You have to use the option Indexes in the Apache configuration.
For instance, in the .htaccess file for your directory (if you can use those, and have sufficient privileges), you could put :
Options +Indexes
This functionality is provided by mod_autoindex, which has lots of options to allow fine-tuning of the generated output, btw
To get this working, that module must be loaded, and to be able to activate the option in an .htaccess file, you will need the admin of the server to give you some privileges (with the AllowOverride directive in the Apache's main config file, if I remember correctly -- well, your admin should know that better than me anyway ^^ )