问题
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.
回答1:
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]
来源:https://stackoverflow.com/questions/12683231/htaccess-rewrite-real-directory-path-to-a-different-non-existing-url-hiding-th