76 votes

How do I get Apache2 to parse (without error) Header directives in a .htaccess?

With apache2, just run a2enmod headers and then sudo service apache2 restart and it will install the headers module automatically.
45 votes
Accepted

How can I redirect an entire site to a single page?

If you are "shutting down a site" then you probably should not be "redirecting" the old site pages to a single page. An HTTP redirect sends a 301 response code, informing users and ...
  • 12.1k
22 votes

Load time impact of htaccess

The recommendation from the Apache project is: In general, you should only use .htaccess files when you don't have access to the main server configuration file. ... a common misconception is that ...
  • 74k
21 votes
Accepted

Break up a long line in a .htaccess file

The following should work: Header set Content-Security-Policy "default-src http://domainA.com; \ script-src http://domainB.com"
20 votes

How should I use the <If> directive in .htaccess?

<If> is only available in Apache 2.4+, so firstly make sure that you have that version. Examples # Compare the host name to example.com and redirect to www.example.com if it matches <If "%{...
18 votes

AH01630: client denied by server configuration Apache

Apache 2.4 comes with some security enhancements that attempt to make it more difficult for hackers to hide their files on a compromised system. This behaviour is explained more at: http://httpd....
  • 380
17 votes

How to redirect root and only root via htaccess?

This should work just fine: RedirectMatch 301 ^/$ https://example.com/
  • 271
17 votes
Accepted

What is the difference between HTTP_HOST and HTTPS_HOST in apache .htaccess files?

There is no such Apache server variable HTTPS_HOST, only HTTP_HOST. If HTTPS_HOST is set on your server then it's specific to your server. The HTTP_HOST server variable contains the value of the Host ...
  • 12.1k
16 votes
Accepted

Redirect loop when forcing HTTPS

Thanks to HBruijn's comment, I understood why I was getting the redirect loop. The traffic from the ELB will always be HTTP as it handles the HTTPS traffic to the user but to the server it's HTTP, so ...
  • 1,489
12 votes
Accepted

Exclusion of a protected sub-url does not work on Apache 2.4?

as "lain" pointed out the apache 2.4 Auth/Access control stuff has changed since 2.2. So I needed to modify it as follows: AuthType Basic AuthName 'Authentication required' AuthUserFile /var/...
  • 393
12 votes
Accepted

Can't figure out a htaccess rule

All this rule does is to add a trailing / to your URLS if there is none and if there is no . in the URI, so https://example.org/test will be redirected to https://example.org/test/, but https://...
  • 98k
11 votes

How to block IP in .htaccess on Apache 2.4

This is another acceptable syntax for .htaccess file: <RequireAll> Require all granted Require not ip 50.62.136.183 </RequireAll> This syntax is recommended for 2.4 because the order-...
  • 211
11 votes
Accepted

Apache 2.4 authenticate anonymous users but allow others by IP

You only need the RequireAny condition: <RequireAny> and </RequireAny> are used to enclose a group of authorization directives of which one must succeed in order for the <RequireAny&...
10 votes
Accepted

Redirecting from http to https in Apache

You should configure Apache Virtualhosts to do the job. RewriteMod isn't the appropriate solution for this case and .htaccess isn't either. In your httpd.conf or equivalent use the following lines ...
10 votes

Break up a long line in a .htaccess file

Yes - the backslash works as a line-continuation. This is buried within the Apache 2.4 documentation on https://httpd.apache.org/docs/2.4/configuring.html#page-header Important rules: Whitespace ...
  • 101
10 votes
Accepted

HSTS and double redirect

As noted on the HSTS preload list submission requirements: Redirect from HTTP to HTTPS on the same host, if you are listening on port 80. You need to redirect to the same host (ie. HTTP_HOST), not ...
  • 12.1k
9 votes

Redirect all http AND https non-www URLS to https://www.example.com via htaccess

@Simbus82 is right about inverting the rules. Here is an approach that is slightly more universal. RewriteEngine On # ensure www. RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^ https://www.%{...
  • 191
9 votes

How to block IP in .htaccess on Apache 2.4

Apache 2.4 makes some big changes in the way it authorizing users. Authorization components in Apache 2.4 can now use the Require syntax that was previously only available for authentication. This ...
9 votes
Accepted

Use of '\' Before DOT in Domain Names in .htaccess Code

Because the second argument to both the RewriteCond and SetEnvIfNoCase directives are regular expressions (regex), not ordinary strings. Regex use a special syntax to define search patterns. In regex ...
  • 12.1k
8 votes

Permalinks in Wordpress not working on Google Cloud Platform

use ssh: edit your apache .conf sudo nano /etc/apache2/apache2.conf <Directory /var/www/> Options Indexes FollowSymLinks AllowOverride ALL <----(edit from none) ...
8 votes
Accepted

.htaccess Redirect 301 with trailing slash adds trailing slash to destination

But that redirects to example.php/ That is caused by the first (original) rule. Note that the earlier (erroneous) 301 will likely have been cached by the browser. Test with 302 (temporary) redirects ...
  • 12.1k
7 votes
Accepted

Permission denied: Could not open password file.

You are having this problem because of SELinux security context. To overcome this you need to change the selinux label of the directory/file in question. You can find out the apache process security ...
  • 8,941
7 votes
Accepted

AddHandler for PHP with different file extension

A workaround/alternative is to simply rewrite the .asp request to .php and store your files as ordinary .php files - assuming .php files are served correctly. Using mod_rewrite you could do something ...
  • 12.1k
7 votes
Accepted

What does <IfModule> do in .htaccess?

<IfModule> is simply a directive that tests the condition "is the named module loaded by apache httpd" (in your example mod_expires). It allows people to produce conditional based configuration ...
  • 2,326
7 votes
Accepted

Apache - Need to rewrite /contact/ to /contact.php

# Internally rewrite "/contact/" to "/contact.php" RewriteRule ^/contact/?$ /contact.php [L] In per-directory .htaccess files you need to remove the slash prefix on the RewriteRule pattern (as you ...
  • 12.1k
7 votes

Can't figure out a htaccess rule

Without validating, but using my experience in Apache rewriting, this configuration seems to: Match on the 'path' part of the URI (not the server, port, or query parameters), for example '/my/...
  • 2,021
7 votes

.htaccess - Exclude folder from basic auth protection

Place a .htaccess file in the media folder with AuthType None. AuthType None Require all granted
6 votes

apache multiviews, how to disable it

In case of Options either you have to use * or - sign with all or with none; i.e. Options FollowSymLinks -MultiViews -Indexes is wrong. If you are using + or - sign, you have to use for all options ...
  • 61
6 votes
Accepted

Apache Adds Trailing Slash Despite RewriteRule

As determined in the comments above, the DirectorySlash directive controls this particular Apache behaviour and the rewriterule did not cause it. This directive defaults to on. If you set it to off, ...
  • 2,326
6 votes
Accepted

Automatically redirecting http to https with multiple domains - files referenced from within code

The Firefox error I am getting is: That's the problem... the browser is triggering this error before the request even reaches your server, so any attempt to redirect on the server is too late. If ...
  • 12.1k

Only top scored, non community-wiki answers of a minimum length are eligible