Note that there are some explanatory texts on larger screens.

plurals
  1. POhtaccess RewriteCond require user
    primarykey
    data
    text
    <p>There is a problem I am having, and I'm pulling my hair out on how to solve it:</p> <p>in root folder I have a htaccess file with the following rules:</p> <pre><code>RewriteCond %{REQUEST_FILENAME} !-s RewriteCond %{REQUEST_FILENAME} !-l RewriteCond %{REQUEST_FILENAME}/index.php !-s RewriteCond %{REQUEST_FILENAME}/index.html !-s RewriteCond %{REQUEST_FILENAME}/index.htm !-s RewriteRule ^(.*)$ redirect.php [QSA] </code></pre> <p>So my goal is to rewrite to <code>redirect.php</code> if the file/directory/index file is not found.</p> <p>This works perfectly well for my entire site, so no problems... until I have a folder that is protected by password with the following htaccess file:</p> <pre><code>AuthUserFile /path/to/htpasswd AuthName EnterPassword AuthType Basic require user adminUserName </code></pre> <p>It seems that apache regards going to this folder as invalid and performs the rewrite to <code>redirect.php</code>.</p> <p><strong>BUT</strong>, if I am already authenticated (by disabling the rewrite above, logging in, then re-enabling the rewrite again), it works...</p> <p>I have tried adding the following to my root htaccess file <em>before</em> the rule I have above:</p> <pre><code>RewriteRule ^secureFolder(.*)$ - [L] </code></pre> <p>And it doesn't work; the following works, but it defeats the purpose of my <code>redirect.php</code>:</p> <pre><code>RewriteRule ^(.*)$ - [L] </code></pre> <p>Any ideas welcome!</p> <p><strong>UPDATE: 2013-10-31 14:00 ET</strong></p> <p>Well, I have given on dealing with it using HTTP auth. So I implemented a simple password system just for that folder using PHP...</p> <p>It goes like this:</p> <p>In <code>.htaccess</code>:</p> <pre><code>&lt;IfModule mod_php5.c&gt; php_value auto_prepend_file /path/to/a/file/in/secureFolder &lt;/IfModule&gt; </code></pre> <p>in <code>.user.ini</code> (for PHP-FPM, if you use it):</p> <pre><code>auto_prepend_file = /path/to/a/file/in/secureFolder </code></pre> <p>In <code>/path/to/a/file/in/secureFolder</code>:</p> <pre><code>session_start(); /** * Check if logged in */ if (!isset($_SESSION['adminLogin'])) { include('/path/to/loginTemplate'); exit; } </code></pre> <p>In <code>/path/to/loginTemplate</code>:</p> <pre><code>&lt;?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { $username = isset($_POST['username']) ? $_POST['username'] : null; $password = isset($_POST['password']) ? $_POST['password'] : null; if ( $username == 'username' &amp;&amp; sha1($password) == 'somehashedvalueofyourpassword' ) { $_SESSION['adminLogin'] = true; header('refresh: 0'); } else { $message = 'Incorrect login'; } } ?&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Login&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;?php if (isset($message)) echo $message ?&gt; &lt;form action="" method="post"&gt; &lt;p&gt; &lt;label&gt;Username:&lt;/label&gt; &lt;input type="text" name="username" /&gt; &lt;/p&gt; &lt;p&gt; &lt;label&gt;Password:&lt;/label&gt; &lt;input type="password" name="password" /&gt; &lt;/p&gt; &lt;p&gt; &lt;input type="submit" value="Login" /&gt; &lt;/p&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And a <code>logout.php</code>:</p> <pre><code>&lt;?php session_start(); unset($_SESSION['adminLogin']); $location = empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/admin.contests/'; header('Location: ' . $location); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload