Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite : Redirect page based on authentication

         

nik545

1:01 pm on Apr 5, 2020 (gmt 0)

5+ Year Member



Hi Webmaster World
im trying to run my own web server and ran into problem
what i need to do :
on main page password form for different users
after login user redirected to their directory
after reading forum found this post [webmasterworld.com ]
managed to do redirects to user dir but i cant download files from it or change to subdir

apache2.conf

<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>


<Directory /var/www/html/ftp_main/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>


<Directory /var/www/html/ftp_main/upload/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>




site.conf

<VirtualHost *:8080>

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/ftp_main


ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined



<Directory "/var/www/html/ftp_main">
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>

</VirtualHost>



.htaccess

AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
RewriteEngine on
RewriteCond $1 !^FOO/
RewriteCond %{REMOTE_USER} ^FOO$
RewriteRule (.*) /var/www/html/ftp_main/upload/ [L]


any one can help? I Have No Idea About This Problem(
BEST REGARDS
NIK545

[edited by: not2easy at 1:30 pm (utc) on Apr 5, 2020]
[edit reason] 'FOO' anonymized for privacy [/edit]

not2easy

1:37 pm on Apr 5, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Hi nik545 and welcome to WebmasterWorld [webmasterworld.com]

You might notice that I have edited the username detail in your code, substituting FOO for the original. We wouldn't want bots sharing anything personal like that.

Sorry but Apache conf. isn't something I work on, there are many others here that can probably help you out. ;)

w3dk

12:11 am on Apr 6, 2020 (gmt 0)

10+ Year Member Top Contributors Of The Month



after login user redirected to their directory
:


RewriteCond $1 !^FOO/
RewriteCond %{REMOTE_USER} ^FOO$
RewriteRule (.*) /var/www/html/ftp_main/upload/ [L]



Has something been "anonymized"-out here?! This looks like it would cause an internal rewrite loop (unless you have something else that controls the "/upload" directory)?

This also isn't a "redirect" to "their" directory? It appears to be a rewrite to a general "/upload" directory?

It's not clear from this what you actually want to do with the requested URL-path, but it looks like you need to include "FOO" in the substitution string at the start of the public URL-path. In .htaccess you shouldn't be including the absolute filesystem path as part of the substitution.



<Directory /var/www/html/ftp_main/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

<Directory /var/www/html/ftp_main/upload/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>



The second Directory container would seem to be superfluous here. But why are these in your main server config and not in your vHost?

You also appear to be duplicating the HTTP authentication block in both your vHost and .htaccess?

lucy24

2:32 am on Apr 6, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You also appear to be duplicating the HTTP authentication block in both your vHost and .htaccess?
A head-scratcher, for sure. If it is your own server and you have access to the config file, why is there an htaccess at all?

Allowing overrides in a single obscure directory is a reasonable compromise: you can then try out new stuff in htaccess without any risk of interfering with the rest of the server if you turn out to have goofed catastrophically. But you wouldn't do it for the site as a whole on a permanent basis.

phranque

8:18 am on Apr 6, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, nik545!

managed to do redirects to user dir but i cant download files from it or change to subdir

what did the download request look like, what kind of response did you expect and what kind of response did you get?
did you look for clues in the server access and error log files?

It's not clear from this what you actually want to do with the requested URL-path, but it looks like you need to include "FOO" in the substitution string at the start of the public URL-path.

probably, but he needs to answer some questions before we know that.

In .htaccess you shouldn't be including the absolute filesystem path as part of the substitution.

unless it is used in a virtualhost context.
see the "file-system path" and "URL-path" descriptions here:
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

You also appear to be duplicating the HTTP authentication block in both your vHost and .htaccess?

won't help, can't hurt.

why is there an htaccess at all?

distracting/irrelevant to the problem statement.

nik545

8:00 am on Apr 7, 2020 (gmt 0)

5+ Year Member



im not sure what i did but its works like charm now
maybe problem was in dir\file permisions, the path /var/www/html/ftp_main/upload/user
is link to /home/user/upload (now its set to 0755)
In .htaccess you shouldn't be including the absolute filesystem path as part of the substitution.

why not ? and what i change them to ? read [httpd.apache.org...] but did not get it , sry im a noob (

now config files are:
.htaccess
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
RewriteEngine on

RewriteCond $1 !^user01/
RewriteCond %{REMOTE_USER} ^user01$
RewriteRule (.*) /var/www/html/ftp_main/upload/user01/$1 [L]

RewriteCond $1 !^user02/
RewriteCond %{REMOTE_USER} ^user02$
RewriteRule (.*) /var/www/html/ftp_main/upload/user02/$1 [L]

RewriteCond $1 !^user03/
RewriteCond %{REMOTE_USER} ^user03$
RewriteRule (.*) /var/www/html/ftp_main/upload/user03/$1 [L]


apache2.conf

DefaultRuntimeDir ${APACHE_RUN_DIR}
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf

<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>

<Directory /var/www/html/ftp_main/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

<Directory /var/www/html/ftp_main/upload/user01/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

<Directory /var/www/html/ftp_main/upload/use02/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

<Directory /var/www/html/ftp_main/upload/user03/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>


AccessFileName .htaccess

<FilesMatch "^\.ht">
Require all denied
</FilesMatch>


LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

site.conf

<VirtualHost *:8080>

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/ftp_main
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

<Directory "/var/www/html/ftp_main">
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>

</VirtualHost>

thank you for your help and sorry for wasting your time )
best reguards )

nik545

2:13 pm on Apr 7, 2020 (gmt 0)

5+ Year Member



Ran to additional problem, how can i setup folder download?)

phranque

12:00 am on Apr 8, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Ran to additional problem, how can i setup folder download?

please describe the problem you are having in terms of:
what did the download request look like, what kind of response did you expect and what kind of response did you get?
did you look for clues in the server access and error log files?

nik545

3:32 pm on Apr 8, 2020 (gmt 0)

5+ Year Member



I need apache as file server, now its working great, i wonder if it's possible to download directory with files in one click