Redirect to page/url, controlled by user in htaccess

Viewed 21

How can I redirect/send users, 'controlled' by username in .htaccess?

Example:

  • If UserA logs in then, UserA is sent to 1234.html
  • IF UserB logs in then, UserB is sent to 9876.html
1 Answers

Assuming you are protecting the root directory with HTTP authentication then you can do something like the following in the root .htaccess file:

RewriteEngine On

# UserA redirected to "/1234.html"
RewriteCond %{REMOTE_USER} =UserA
RewriteRule !^1234\.html$ /1234.html [R,L]

# UserB redirected to "/9876.html"
RewriteCond %{REMOTE_USER} =UserB
RewriteRule !^9876\.html$ /9876.html [R,L]

If UserA is logged in and accesses this directory then they are redirected (302 - temporary) to /1234.html. Otherwise, if UserB is logged in they are redirected to /9876.html.

The CondPattern, eg. =UserA (= prefix), is an exact string comparison for "UserA" (case-sensitive).

The negated (!) RewriteRule pattern, eg. !^1234\.html$ checks that the request is not already for 1234.html (ie. the target file), otherwise we would get a redirect loop.

Note that the user is always redirected (forced) to the respective file/page. If the user should only be redirected to the file when they first login then you will need another condition (perhaps based on a cookie) to determine that they have already visited that file.

Related