skip to Main Content

I’m trying to do something that I think should be simple, but I can’t get it to work…

I want to set up my .htaccess file so that any requests to a directory (or to any file within that directory or subdirectory) will actually return a file in a different directory.

In other words, any of the following requests (with or without query parameters):

/dummy
/dummy/
/dummy/foo
/dummy/foo/
/dummy/bar.html
/dummy/foo/bar.html
/dummy/abc/def/ghi/jkl/boo.txt

should all return the file at

/testing/test.php

Note that the /dummy directory doesn’t actually exist at all.

Ideally, it shouldn’t be possible to return the /testing/test.php file directly, but that’s not a dealbreaker. However, if query parameters are included in the original request, then they should be passed to /testing/test.php. It would be great if the PHP code in /testing/test.php could determine what the original request URI was, but that’s really a separate PHP question.

I found a similar question at Using Apache mod_rewrite to send all requests to a file which linked to Create blog post links similar to a folder structure, but I can’t seem to get it to work in my case, even though I’ve been playing with the assorted directory values.

2

Answers


  1. Possibly something like this

    RewriteEngine On 
    Options +FollowSymLinks
    RewriteRule ^dummy/(.*) testing/test.php?_REQUEST=$1 [NC,L]
    
    Login or Signup to reply.
  2. You may use these rules in site root .htaccess:

    RewriteEngine On 
    
    # prevent direct access to /testing/test.php
    RewriteCond %{THE_REQUEST} s/+testing/test.php[?s] [NC]
    RewriteRule ^ - [F]
    
    # rewrite everything that starts with /dummy to /testing/test.php
    RewriteRule ^dummy(/.*)?$ /testing/test.php [NC,L]
    

    Note that query string in original request will automatically be forwarded to /testing/test.php. Also you can get original request URI using php variable:

    $_SERVER['REQUEST_URI']
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search