I am shutting my website down and I am needing the website to stay live for the next 3 months. What I am wanting to do is basically only let registered users login. The rest of the website will be off limits. How can I set it up to where no matter what page the person goes to, it will always redirect them the "Login" page?
-
hard to believe you will be able to solve with .htaccess but try to be more specific on what technologies are you using, how the website is structured and if you have some method to check a user is authenticated. – Narcis Radu Jan 30 '12 at 23:33
5 Answers
You could use a simple redirect I suppose:
<?php
header("Location: http://www.example.com/login_page.php");
exit;
?>

- 734
- 2
- 7
- 27
i'd say it depends on how the entire site is set up. If all the pages have something in common (like a master page in asp), you could place in that master page code something that attempts to check the session state. If the session state doesn't contain some form of authentication (like the kind you use when a user is logged in), then redirect the user to the login page.

- 11,426
- 28
- 107
- 176
Try:
RewriteEngine On
RewriteCond %{REQUEST_URI} !=/login_page.php
RewriteRule ^ /login_page.php [R=301]

- 1
- 1

- 8,903
- 3
- 19
- 19
-
This would redirect users who have already visited `login_page.php` and logged in, back to the login page, would it not? – Ben Swinburne Jan 30 '12 at 23:45
-
Good thinking. I didn't think that far ahead. Should I delete this post or just leave it? – shaunsantacruz Jan 30 '12 at 23:48
If you are using Apache, you can create a .htaccess
file in the root of your website with the following contents.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule (.*) http://www.yournewwebsite.com.com/login.php [R=301,L]
</IfModule>
However, beware that this file will have an effect on every page on your website - including the login page.

- 930
- 7
- 16
I would suggest you add in a bit of functionality to your current set up, where you can add a login script first.
Example:
<?php
include ("login.php");
if($logged_in === false){
//show login page.
exit();
}
//then rest of code goes here.
This way, your site still has all it's pages, but now you can limit to only people allowed to log in.

- 672
- 5
- 12