host-specific .htaccess using if

htaccess example
htaccess rewrite
htaccess php
how to create htaccess file in php
htaccess file generator
htaccess tutorial
htaccess redirect
how to change url using htaccess in php

On a service available through different subdomains/domains, I want to be able to use only one .htaccess file. For each possible value of %{HTTP_HOST}, I have set up if blocks like this:

<if "%{HTTP_HOST} == 'staging.project.example.org'">
    [detailed configuration...]
</if>

<if "%{HTTP_HOST} == 'development.project.example.org'">
    [detailed configuration...]
</if>

This works well with access configuration lines like:

<if "%{HTTP_HOST} == 'staging.project.example.org'">
    AuthType Basic
    AuthUserFile ".htusers"
    Require user example
    Order deny,allow
    Deny from all
   Allow from xxx.xx.xx.xxx
</if>

But RewriteRule lines like this will be ignored by Apache:

<if "%{HTTP_HOST} == 'production.project.example.org'">
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} !^/maintenance\.html$
    RewriteRule ^(.*)$ https://www.project.example/maintenance.html [R=307,P]
</if>

Is it possible to use RewriteRule with if in .htaccess? I think it might be because of

"Only directives that support the directory context can be used within this configuration section." in https://httpd.apache.org/docs/2.4/en/mod/core.html#if

but is there a way around this?

I don't think rewrite rules can work in an if directive, but you can include the same logic in the rewrite rule itself:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} production.project.example.org
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteRule ^(.*)$ https://www.project.example/maintenance.html [R=307,P]

The RewriteCond statements form a logical AND giving you the same logic as if you encapsulated in if directives

2020 Complete Guide To .HTACCESS – From The Basics To , If you are completely new to using .htaccess — you might want to start with the first If you are searching for specific code samples or tutorials look at the Apache webserver used by most commercial web hosting providers. Exactly how you should create a redirect in [code ].htaccess[/code] can depend on other factors. For instance, the specific URL you are wanting to redirect (eg. whether the URL contains a query string) or whether you have existing mod_rewrite dire

If and mod_rewrite in some cases can play the exact same roles, for instance, in mod_rewrite an "If" would be like RewriteCond.

So.. do not mix them together, use one, or the other.

htaccess Files, If you want to call your .htaccess file something else, you can change the name of the file using the AccessFileName directive. Context: server config, virtual host, directory, .htaccess. Override: FileInfo. If you are unsure whether a particular directive is permitted in a .htaccess  One of the many functions you can perform via .htaccess is the 301 redirects, which permanently redirects an old URL to a new one. You can activate the feature to force HTTPS on all incoming traffic by following these steps: Go to File Manager in your hosting panel and open .htaccess inside the public_html folder. If you can’t locate it, make

To answer my own question: For host-specific rules within the same .htaccess file, I did this, which uses a logic similar to this other answer.

SetEnvIfNoCase Host staging.project.example.org $ require_auth=true
AuthType Basic
AuthUserFile "/path/to/your/.htusers"
Order Deny,Allow
Deny from env=require_auth
Allow from xxx.xx.xx.xx # to allow certain IPs without login
Require user ... # users from the .htusers file above that are allowed to login
Satisfy any

Using this setting, any access using the host staging.project.example.org will need to go through authentication or match a whitelisted IP.

Multiple lines of SetEnvIfNoCase are possible.

How can I redirect and rewrite my URLs with an .htaccess file , htaccess file knows where it's located. Also, if you see the domain 'example.com', change this to your own domain name. Redirecting all URLs. Use a RewriteCond for %{HTTP_HOST} if you really need it to be host-specific Your rules are in per-directory or htaccess context, and your URL maps to that place in the filesystem via an Alias, but you either left the RewriteBase off or used the filesytem path and not the URL-path as the argument to RewriteBase.

Htaccess, This type of hosting is called "virtual hosting" and without .htaccess files If you do have access to your main server configuration file, you should of An .​htaccess directive is basically a command that is specific to a module  UnitedHositng (UK) From user kemmish: Open the gallery3/.htaccess file in wordpad and at the very bottom of that file it says to not use the Index.php uncomment the following lines of code. So if you in comment the last few lines of code in that file then save and upload the file overwriting the old .htaccess file that you just modified.

How To Redirect Rewrite Using The htaccess File - www , How To Redirect Rewrite Using The htaccess File - www to non-www that the next rule only fires when the http host (that means the domain of the queried url) is not (- specified with the "! If not, this can be any character(but only one). So . Host specific issues zend guard. From Gallery Codex. One cannot disable zend guard on a shared server using php.ini or .htaccess [my results anyway]

Making sure .htaccess and mod_rewrite are working as they should , Bolt makes extensive use of a common feature called 'url rewriting'. The simplest way to test if apache uses your .htaccess file, or if it otherwise ignores it, Check your virtual host configuration and add/amend to AllowOverride All . Implementing Search · Template Specific Fields · Using widgets · Twig tags, functions &  NOTE: There are 5 settings / directives that cannot be changed in .htaccess files using php_flag and php_value and can only be changed by your Web Host in the php.ini file or httpd.conf Server file – allow_url_fopen, disable_functions, expose_php, report_memleaks and mysql.allow_persistent. Changing the settings for any of these 5 directives will have no effect and they will be overridden by the Server’s php.ini file.

Comments
  • Mod_rewrite does support the directory context, so it passes that criteria, although I'm not saying it should definitely work. When you say it doesn't work, in what way? What happens when you try it?
  • Nothing happens - the RewriteRules inside ifare ignored.
  • you should just concatenate "If" statements or declare and if that checks for both things, also, do not mix P and R flags in mod_rewrite since they are meant for different things, do you want to redirect or rev proxy?
  • instead of flagging my comment for removal, at least try to fix the R,P flags in your answer, stackoverflow doesn't let me because its just 2 characters.