Rewrite rules to hide an url

14,466

As I explained in : How does url rewrite works? every rewrite triggers a new call to the rewritten URL. (HTTP 3xx code).

So the client will ask for www.site.com/this/is/path/to/hide/page.php, would be redirected to www.site.com and will be served the index page as a normal user.

There is no way to tell the client to display one URL in the browser bar instead of another, client browser will always make a new request. (Or you could impersonate any site for example)

If you are not against the use of a cookie, or can use environment variable you may be able to do something like :

RewriteRule this/is/path/to/hide/page.php / [co:knowHiddenPath=true] 

The environment variable as same syntax with E instead of co.

(See http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html for cookie information)

Your index page should then check this cookie/variable to serve the hidden page or not.

Another solution would be to enable access with password to your file. So even if someone know the URL he would not access the file. Security by obscurity is does not exists.

Share:
14,466
elp
Author by

elp

code lover. linkedin || twitter || blog

Updated on June 04, 2022

Comments

  • elp
    elp almost 2 years

    i need to hide full path and show shortly:

    www.site.com/this/is/path/to/hide/page.php --> www.site.com

    Any idea to do it with .htaccess apache and rewrite rules??

    EXAMPLE:
    If i type www.site.com i want open index.php (in /),
    but if i go to /hidden/path i want to open file.php (in hidden/path)
    mantaining browser url in www.site.com.


    EDIT:
    i want to see in the browser bar www.site.com and i want to open page at /this/is/path/to/hide/page.php .

    thanks

  • ssapkota
    ssapkota almost 13 years
    Now the only thing that you would show is the the file: page.php. Yes path will be still be hidden.
  • elp
    elp almost 13 years
    VirtualHost is not in htaccess, true? What kind of file that i need to do? Where?
  • elp
    elp almost 13 years
    @LazyOne Ok, but i need to do exactly the opposite! I go to /hidden/path.php and i want see www.mysite.com. thanks.
  • LazyOne
    LazyOne almost 13 years
    @Paska What do you mean by "see"? You want to be redirected to the root -- e.g. you type that path in the browser and it redirects you to the root and you see just www.mysite.com in the browser bar? Or you want that URL stays the same in address bar (as you typed) but it will show you (in the browser) the home page?
  • elp
    elp almost 13 years
    Sorry, i want to see in the browser bar www.site.com and i want to open page at /hidden/path.php.
  • LazyOne
    LazyOne almost 13 years
    @Paska So you type www.site.com but want to see the www.site.com/hidden/path.php instead and browser should keep URL as is. Is that correct?
  • elp
    elp almost 13 years
    No, if i type www.site.com i want see index.php (in /), but if i go to /hidden/path i want to see file.php (in hidden/path) mantaining browser url in www.site.com.
  • LazyOne
    LazyOne almost 13 years
    @Paska That does not work just like that. It has to be something else involved. You cannot do what you want by just doing URL rewriting. No. Maybe you can show some real example (from real site) so I can look and understand better what you want to achieve and maybe explain you how they did it?