Forum Moderators: phranque

Message Too Old, No Replies

Mod_Rewrite

Virtual directory structure with help of PHP

         

Anthrax

4:00 pm on May 18, 2004 (gmt 0)

10+ Year Member



Hello, everybody!
I've tried to get virtual directory structure for client-browser and my own directory structure for me.

Example
===
- client type in his browser:
www.site.com/dir1/dir2/dir3/doc.html

- and browser redirects him to:
www.site.com/index.php

BUT! URL already typed in Address_Line (in browser) have to stay like original ('mod_rewrite' can do it?)

Please, can you advise me how to solve such a problem?

gergoe

4:29 pm on May 18, 2004 (gmt 0)

10+ Year Member



Certainly; Check out the mod_rewrite documentation [httpd.apache.org] for the syntax and the setup of mod_rewrite and the URL Rewriting Guide [httpd.apache.org] for practical solutions. I think you'll find an example implementation in the guide which you need to just adjust a bit to fit your needs.

Anthrax

9:09 am on May 19, 2004 (gmt 0)

10+ Year Member



I've already read it, but all i found is that mod_rewrite "changes" the original url in browser-line to url i've constructed in RewriteRule with RewriteCond.

But i need "original" url stay without any changes and at the same time my script starts.

So don't care what i've type in browser line:
- www.site.com/dir1/
- www.site.com/1.html
- www.site.com/dir2/dir3 (without trailing slash)

i need this lines (urls) stay unchanged.
And only this script (www.site.com/index.php) will get all this requests.

gergoe

1:43 pm on May 19, 2004 (gmt 0)

10+ Year Member



Based on the substitution url you specify and based on the flags you use for the RewriteRule, mod_rewrite decides how to process the url. If you just put the /index.html into the substitution string and you don not define none of the R and P flags, then it will be rewrite, so the url will be rewritten silently (without changing the url in the address bar). But if you specify the R flag, then it will be redirection (so the url will change in the address bar), but then the substitution string must be a valid url either a relative (/index.html) either absolute (http://www.domain.com/index.html). Additionally if you have mod_proxy enabled, then you can specify the P flag, so the substitution string will be fetched by mod_proxy via a sub-request, and the result is returned to the browser. In this case the substitution string must be a fully qualified url (http://www.domain.com/index.html).
I recommend you to read the documentation of the RewriteRule directive [httpd.apache.org], it has all the information you need to start using it in the way you want.

Anthrax

2:18 pm on May 19, 2004 (gmt 0)

10+ Year Member



Thank you Gergoe!
It was exact i need to know about silent substitution.

Anthrax

9:33 am on May 20, 2004 (gmt 0)

10+ Year Member



I've tryed this one lines in my .htaccess file

---
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^(.*)$ [localhost...] [L]
---

But redirection is not silent. I mean that url in browser changes :(((

Some ideas?

gergoe

10:42 am on May 20, 2004 (gmt 0)

10+ Year Member



If you want to rewrite the url, then you don't need to use fully qualified url, the path information is fairly enough. Like this:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /start.html [L]

In the case if the (virtual) host where you've placed these directives is other than localhost, then you need to use mod_proxy together with the mod_rewrite, so the url first will be fetched from the remote host by mod_proxy, and the result will be returned to the browser.

But if you want to make this rewriting only to display a page when a resource is not found, then use the ErrorDocument instead. See [httpd.apache.org...]

Anthrax

10:50 am on May 20, 2004 (gmt 0)

10+ Year Member



Thanks a lot! It's working greate!
For me now it's enough with "localhost", but for future use - "mod_proxy" will help i'm sure.