Forum Moderators: phranque
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
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
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
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]
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
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
RewriteRule (index¦calculator¦guide¦contacts)\.html$ http://www.example.com/%1/index.php?action=%2 [R=301,L]
Jim