问题描述
我想使用像/___test/subdirectory1/
这样的目录___test
并具有一些内容,但是永远不要在url中显示此路径.相反,我希望URL /demo/subdirectory1
或说/demo/subdirectory1/images/image.jpg
指向/___test/subdirectory1/images/image.jpg
,浏览器上的URL保持如下:/demo/subdirectory1/images/image.jpg
.实际将___test
替换为demo
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
以上仅适用于:demo/subdirectory1
或demo/subdirectory1/
(现有内容示例为/___test/subdirectory1/
内的index.html),但不适用于demo/subdirectory1/somethingmore...
,尽管在这种情况下也存在该内容,但不幸的是,它显示了网址中的真实目录路径.
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.
此外,我正在使用以下代码通过404到url中从/___test
开始的任何内容:
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]
它有效,但是当我添加它时,不幸的是(同样),前一个也转到了404.但是对我来说最重要的是,即使我从未做过第二部分,也要正确设置第一部分.
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.
我也尝试在/___test/subdirectory1/.htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteBase /___test/subdirectory1
没有成功.
尽管效果不佳,但到目前为止,在乔恩·林的帮助下,我得出的最好成绩是:
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
<-确定
/demo/subdirectory1/
<-确定
/demo/subdirectory1/subdirectory2
<-公开真实路径
/demo/subdirectory1/subdirectory2
<- Exposes real path
/demo/subdirectory1/subdirectory2/
<-确定
/demo/subdirectory1/subdirectory2/subdirectory3
<-公开真实路径
/demo/subdirectory1/subdirectory2/subdirectory3
<- Exposes real path
/demo/subdirectory1/subdirectory2/subdirectory3/
<-确定
如您所见,比subdirectory1
更深的层次都有问题.我只是不明白为什么.在Linux apache站点根目录中存在的一个干净的.htaccess文件中进行测试(没有其他更深入的说明).
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.
推荐答案
在大多数情况下,您都处于正确的轨道,但是您可能需要添加一些检查,以免暴露__test
目录:
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]
确保已使用[L]
在当前迭代中结束重写.
Making sure that you've ended rewriting in the current iteration by using [L]
.
您的404规则应该可以工作,但是您应该删除斜杠,以便mod_dir不会尝试重定向并公开___test
目录:
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]
这篇关于htaccess将真实目录路径重写为其他不存在的url,隐藏真实路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!