Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite redirect to parent directory problem

I am having problems redirecting to parent directory

         

GCharb

12:58 pm on Apr 6, 2008 (gmt 0)

10+ Year Member



Hello all!

Building a php script where users in a sub directory choose a link in a menu and I need mod rewrite to redirect to a php script in the parent directory where part of the menu url would be used as a value for an action. Here is my .htaccess snipet.

Options +FollowSymLinks
RewriteEngine on

# redirect all *.html and such to index.php in the parent directory
RewriteRule ^(index¦calculator¦guide¦contacts*)\.html$ ../index.php?action=$1 [L]

Gives me a 404, anyone has a suggestion?

Thanks

jdMorgan

2:37 pm on Apr 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cannot use "relative links" in mod_rewrite. Such links are usable on Web pages only because the client (browser) uses the current URL (the one in its address bar) to resolve relative links, but mod_rewrite has no such capability.

In theory, the server could use the HTTP referrer to resolve relative URLs in the same way as a browser, but unfortunately, transmission of the HTTP referrer from the client to the server is optional, and therefore this method would fail often.

The easiest way I can think of to resolve the "parent directory" in an .htaccess context is to use THE_REQUEST, which contains the entire HTTP request line sent by the client -- For example, "GET /foo/bar/user/action.html HTTP/1.1"


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*[^/]+/(index¦calculator¦guide¦contacts)\.html\ HTTP/
RewriteRule (index¦calculator¦guide¦contacts)\.html$ /%1/index.php?action=%2

This strips the final directory path (the "user" directory), uses the remaining directory path as the "parent" directory, adds "index.php" and then appends the "html file" as the query string.

It will work at any directory level below the root -- That is, anywhere that a "parent" directory exists.

The redundant patterns, missing start anchor on the rewriterule pattern, and all other changes are intentional.

I removed the "*" from "contacts" because it wasn't clear what that was for. With that quantifier, the pattern would match "contact", "contacts", "contactss", or even "contactssssssssssssssss" and that's not likely what was intended. If you want to match only "contact" or "contacts", the proper pattern would be "contacts?", where the "?" quantifier means, "zero or one of the preceding character."

Change the broken pipe "¦" characters to solid pipes before use; Posting on this forum modifies the pipe characters.

Also, be aware that this is not a redirect, it is an internal rewrite.

Jim

GCharb

6:26 pm on Apr 7, 2008 (gmt 0)

10+ Year Member



Hello Jim

First of, thanks for your help and sorry for the late reply, but I wanted to decrypt your code before I replied.

If I get it right your code does two things, first, it writes a condition where it gets the full request url, in my case it looks alot like /home/mydomain/public_html/sub1/sub2/
Then it strips the non usefull part of that url and makes a rule pointing to the new url?

In any cases it does not work here but it might be because your condition has fewer levels then my actual url. I am working on it.

Gilles

[edited by: GCharb at 7:08 pm (utc) on April 7, 2008]

jdMorgan

9:56 pm on Apr 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



THE_REQUEST is the exact request string you see in your raw log files. It comprises an HTTP method, a URL-path, and a protocol. Therefore it might look like this:
[quote] GET /sub1/sub2/guide.html HTTP/1.1 [/code]

The code, as written above, will rewrite that, making it into a file request for

/home/mydomain/public_html/sub1/index.php?action=guide

where /sub1 is the "parent" directory of /sub1/sub2 -- the requirement I interpreted from your original post.

Jim

GCharb

10:14 pm on Apr 7, 2008 (gmt 0)

10+ Year Member



Hello Jim and thanks again!

I see, got a misconception here because I tried outputing the request alone and I got the whole /home/mydomain/public_html/sub1/sub2/ string.

Now, i have put your code in a .htacces file and tried to put it in both parent and sub dir (one at a time) and the browser does not get redirected to the parent folder, it stays in the subdir, so I must be doing something wrong!

I will work on that, thanks again!

Gilles

jdMorgan

10:26 pm on Apr 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you really want to redirect the client browser (and change the address in the address bar), then you'll need to change the syntax and add a redirect flag to the rule:
 
RewriteRule (index¦calculator¦guide¦contacts)\.html$ http://www.example.com/%1/index.php?action=%2 [R=301,L]

Again, change the broken pipe "¦" characters to solid pipes before use; Posting on this forum modifies the pipe characters.

Jim

GCharb

11:04 pm on Apr 7, 2008 (gmt 0)

10+ Year Member



That's the thing, I want the original url to stay the same, as it is i'll probablly put the php script in all the subdirs and be done with it, thanks a bunch for your time!

Gilles