Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite not redirecting to relative paths

Tried my best to fix this issue but no Success :(

         

gkpruthi

8:47 am on Aug 9, 2007 (gmt 0)

10+ Year Member



Hi,

I got a lot of help from this forum to use mod_rewrite in .htaccess and redirection with user authentication. But still facing issues realted to relative paths. Here is .htaccess
RewriteEngine on
RewriteCond $1!^user_
RewriteCond %{REMOTE_USER} ^([0-9.0-9.0-9.0-9]+)$
RewriteRule (.*) hello/%1/index.html [L]
AuthType Basic
AuthuserFile /usr/local/.htpasswd
AuthName "PROTECTED"
require valid-user
AuthType Basic
deny from all

I am using IP address as user (e.g. 192.168.199.1). Redirection is working perfectly and it takes me to the required page. But i have few links on the opened page and also some images which donot appear. Whenever i click on any link on the page it keeps me on the same page. I guess due to
RewriteRule (.*) hello/%1/index.html [L]

Can anybody Help?

I guess Jim can help me Out :)

Thanks,
Gaurav

gkpruthi

10:52 am on Aug 9, 2007 (gmt 0)

10+ Year Member



Pls ignore last two lines of .htaccess i.e.

AuthType Basic
deny from all

jdMorgan

5:01 pm on Aug 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The RewriteCond patterns appear to be wrong. I'd suggest:

RewriteCond $1 !^hello_
RewriteCond %{REMOTE_USER} ^([0-9.]+)$
RewriteRule (.*) hello/%1/index.html [L]

The purpose of the first RewriteCond is to prevent an infinite loop, so its pattern should match the initial part of the substitution URL in the RewriteRule. The original pattern for the IP address was redundant, and I show a simple generic pattern. A more-specific pattern would be ^(([0-9]{1,3}\.){3}[0-9]{1,3})$ -- That is, 3 groups of ( (one to three digits) followed by a literal period ), and those three groups then followed by one to three additional digits as in "123. 45. 6. 78"

Consider two things:

1) You are rewriting *all* URLs to hello/<Remote-IP>/index.html, so that means that requests for all images, CSS files, external javascripts --everything-- will be rewritten to your .html index page. That's probably not what you want.

2) If a remote user's IP address changes, he will no longer be able to access his files. You might consider a different method of mapping users to subdirectories. Using per-user subdomains is a popular method.

Jim

gkpruthi

5:44 am on Aug 10, 2007 (gmt 0)

10+ Year Member



Thanks Jim. But as I am new to mod_rerewrite, I have no idea to fix the first issue. I have changed .htaccess aa bit as per you have written. Now it looks as:

RewriteCond $1!^hello_
RewriteCond %{REMOTE_USER} ^([0-9.]+)$
RewriteRule index.html hello/%1/index.html [L]

Links on index.html page are still not working. All the links are relative to index.html.

192.168.199.1
--- index.html
--- #*$!x.html
--- yyyy.html

I have put a link for #*$!x.html & yyyy.html on index.html page. All these pages resides on the same directory i.e. "192.168.199.1". But i can't still open the link. When i click on link, it takes me to abcd.com/#*$!x.html which doesnot exist, rather it should take me to abcd.com/hello/192.168.199.1/#*$!x.html

Please help me in fixing this issue. Shall be very grateful to you. :)

Thanks,
Gaurav Pruthi

jdMorgan

3:48 pm on Aug 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's all a matter of deciding which URLs you do and do not wish to rewrite. After defining these requirements, then the coding part is easy. If you haven't done it before, yes, it can be challenging, but the hard part is defining the requirements, and the easy part is the code (after some practice, of course). :)

If you want to rewrite *All* requests from *every* remote_user to separate subdirectories, then the following code will do that:


RewriteCond $1 !^hello
RewriteCond %{REMOTE_USER} ^([0-9.]+)$
RewriteRule (.*) hello/%1/$1 [L]

Please note that I removed the trailing underscore from "hello" in the RewriteCond. I am not sure how it got added, but if you need it, then the substitution URL must also include it -- the RewriteCond pattern must match the substitution URL prefix in order to prevent a loop.

It's likely that there will be some URLs that you do not wish to rewrite. Just for an example, let's assume that you do not wish requests for robots.txt to be rewritten to the user subdirectories. In that case, you can add further exclusions:


RewriteCond $1 !^robots\.txt$
RewriteCond $1 !^hello
RewriteCond %{REMOTE_USER} ^([0-9.]+)$
RewriteRule (.*) hello/%1/$1 [L]

Jim

gkpruthi

7:02 am on Aug 14, 2007 (gmt 0)

10+ Year Member



Hi Jim,

Sorry for late reply as i was out of town and could not connect to internet. Thanks a lot. I used your code and now everything is working as i wished.

YOU ARE SIMPLY GREAT :)

Thanks,
Gaurav Pruthi

gkpruthi

12:00 pm on Aug 16, 2007 (gmt 0)

10+ Year Member



Hi Jim,

One more query. Things are working pretty well but having little problem. I am getting redirected to the user's directory and all links on the page are working perfectly too. Now the issue is that like if i am accessing the following link

[abcd.com...] whose actual path is Document_root/hello/192.168.199.1/192.168.199.1_user.html

Now when i am logged in. If i change the URL from
[abcd.com...]
to
[abcd.com...]

I am able to access the page. Please note I have changed the IP from 1 to 2. How can i forbid the user to access other directories?

Thanks,
Gaurav

jdMorgan

5:50 pm on Aug 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well thanks for the enthusiasm, Gaurav... :)

Since users should never be aware of the "/hello/" directory-path, it may be as simple as denying any direct client requests for any "/hello/" paths, while still allowing requests to be internally-rewritten to that directory-path:


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /hello/
RewriteRule ^hello/ - [F]

THE_REQUEST is the entire request-header received via HTTP from the client. For example:
GET /hello/192.168.199.2/192.168.199.2_user.html

in your example.

I'm actually not sure why/how your example URL resolves. I mean, I would expect a 404 error for that URL, even if you wanted to allow access to URLs like it. Turn off MultiViews, mod_speling, and AcceptPathInfo unless you need them.

Jim

gkpruthi

1:35 pm on Aug 17, 2007 (gmt 0)

10+ Year Member



Thanks Jim.

Actually i have few links on the pages which can point to hello folder and then IP Folder. I am removing them from page as they are not needed anymore. This will prevent users to guess exact URL.

It was really a great help from your side. This forum is very helpful for IT techies.

Wish you all the best :)

Thanks,
Gaurav Pruthi