0

I would like to use a directory ___test like /___test/subdirectory1/ and have some contents, but never show this path in the url. Instead I would like the url /demo/subdirectory1 or say, /demo/subdirectory1/images/image.jpg to point to /___test/subdirectory1/images/image.jpg and the url on the browser to stay like: /demo/subdirectory1/images/image.jpg. Actually replacing ___test with demo

What I have tried with various problems although it looks like it works sometimes is:

RewriteRule ^demo/subdirectory1(.*) ___test/subdirectory1/$1

The above works only on: demo/subdirectory1 or demo/subdirectory1/ (with existing content example an index.html inside /___test/subdirectory1/) but not on demo/subdirectory1/somethingmore... although the content is there in this case as well, unfortunately it shows the real directory path in the url.

Additionally, I am using the following to through 404 to anything starting from /___test in the url:

RewriteCond %{THE_REQUEST} ^GET\ /___test/
RewriteRule ^___test/(.*) - [R=404,L,NC]

It works, but when I add this, then the previous goes to 404 too, unfortunately (again). But most important for me is to make the first part right, even if I never make the second work.

Any help really appreciated. Thanks.

Edit 1: I have also tried adding the following inside an /___test/subdirectory1/.htaccess

Options +FollowSymLinks
RewriteEngine On
RewriteBase /___test/subdirectory1

no success.

Edit 2: Although it doesn't work well, the best I came up with so far with the help of Jon Lin is the following:

RewriteCond %{REQUEST_URI} ^/demo/subdirectory1(.*)
RewriteCond %{DOCUMENT_ROOT}/___test/subdirectory1/%1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/___test/subdirectory1/%1 -d
RewriteRule ^ /___test/subdirectory1/%1 [L]

/demo/subdirectory1 <- OK

/demo/subdirectory1/ <- OK

/demo/subdirectory1/subdirectory2 <- Exposes real path

/demo/subdirectory1/subdirectory2/ <- OK

/demo/subdirectory1/subdirectory2/subdirectory3 <- Exposes real path

/demo/subdirectory1/subdirectory2/subdirectory3/ <- OK

As you can see, whatever is deeper level than subdirectory1 has problem. I just cannot understand why. Testing this on a clean .htaccess file existing in the root of the site (no other deeper) on a linux apache.

durduvakis
  • 189
  • 4
  • 16
  • we seem to have the same issue: http://stackoverflow.com/questions/16248674/how-to-write-htaccess-rewrite-for-subdirectories-recursively?noredirect=1#comment23249673_16248674 – Thomas Cheng Apr 27 '13 at 13:23

1 Answers1

0

For the most part you're on the right track, but you may need to add a couple of checks so that the __test directory isn't exposed:

RewriteEngine On

# prevent mod_dir from redirecting to trailing slash
RewriteCond %{REQUEST_URI} ^/demo/subdirectory1/(.*[^/])
RewriteCond %{DOCUMENT_ROOT}/___test/subdirectory1/%1 -d    
RewriteRule ^ /demo/subdirectory1/%1/ [L,R=301]

RewriteCond %{REQUEST_URI} ^/demo/subdirectory1$
RewriteRule ^ /demo/subdirectory1/ [L,R=301]


# check if the file actually exists first
RewriteCond %{REQUEST_URI} ^/demo/subdirectory1/(.*)
RewriteCond %{DOCUMENT_ROOT}/___test/subdirectory1/%1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/___test/subdirectory1/%1 -d

# and rewrite only if the resource exists within ___test
RewriteRule ^ /___test/subdirectory1/%1 [L]

Making sure that you've ended rewriting in the current iteration by using [L].

Your 404 rule should work but you should remove the trailing slash so that mod_dir won't try to redirect and expose the ___test directory:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /___test
RewriteRule ^ - [R=404,L,NC]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • thanks for your answer indeed, I just tried both things you gave me (only the first part, or both), and in both cases, the real path is exposed again. I would like to mention that I use an absolutely clean .htaccess file in clean directories. Only the index.html files are there for testing. – durduvakis Oct 02 '12 at 01:33
  • @durduvakis you don't have any other htaccess files besides the one in the document root, right? Other than that, I can only think that mod_dir is redirecting. – Jon Lin Oct 02 '12 at 01:36
  • hang on! Embarrassed now, my bad, I was testing your code on the wrong firefox tab. Let me check again. Yes I only got an .htaccess in the document root and is a linux ubuntu server not windows – durduvakis Oct 02 '12 at 01:38
  • ok so if I use this url: `/demo/subdirectory1` I get `Not Found` if I use this: `/demo/subdirectory1/` success. Now if I use: `/demo/subdirectory1/subdirectory2` I get the real path exposed plus `Not Found`, and if I use: `/demo/subdirectory1/subdirectory2/` it success again. – durduvakis Oct 02 '12 at 01:45
  • I re-tested with your edited code, I was testing with your first. Your new code returns `Not Found` to all cases. :( – durduvakis Oct 02 '12 at 01:50
  • yes I keep trying your edits, at one point it as almost perfect it was just not redirecting when url was `/demo/subdirectory1` without trailing slash. Right now it just adds about 10 slashes at the end. I keep checking. Thanks alot ! – durduvakis Oct 02 '12 at 02:00
  • @durduvakis guess trying to combine the 2 exceptions isn't working, just made it 2 different ones. – Jon Lin Oct 02 '12 at 03:13
  • thanks for the help, I was trying different things till now with your code suggestions, the only problem is with the deeper paths like `/demo/subdirectory1/subdirectory2`. The best I came up with so far was by commenting out the first mod_dir part which was causing the many slashes. It worked excellent in all cases even on `/demo/subdirectory1/subdirectory2/` but not `/demo/subdirectory1/subdirectory2` (without trailing slash). It was the only url that was exposing the real path. – durduvakis Oct 02 '12 at 03:32