问题描述
我有一个域,其中包含带有Web应用程序结构的子文件夹.我在根域上添加了 .htaccess
,以指向子文件夹Web应用程序上的公用文件夹.它工作正常,但是当我键入 www.example.com
时,浏览器URL会更改为 www.example.com/subfolder/public
,但我希望它不会"改变.
I've a domain that contains a subfolder with the web app structure. I added a .htaccess
on my root domain to point the public folder on my subfolder web app. It works fine, but when I type www.example.com
the browser URL changes to www.example.com/subfolder/public
, but I would like that it doesn't change.
这是我的 .htaccess
:
RewriteEngine On
RewriteRule ^.*$ subfolder/public [NC,L]
编辑
第一个.htaccess文件用于重定向子文件夹/公共文件夹,在该文件夹下,另一个.htaccess文件可以完成所有工作.
EDIT
This first .htaccess is used to redirect on subfolder/public, where there is an other .htaccess that makes all the works.
这是位于 www.example.com/subfolder/public/
上的第二个.htaccess的代码:
Here the code of the second .htaccess located on www.example.com/subfolder/public/
:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
推荐答案
对不起,刚刚意识到发生了什么事.如注释中所述,它与子目录中的第二个 .htaccess
文件无关.
Sorry, just realised what is happening. It has nothing to do with the second .htaccess
file in the subdirectory, as mentioned in comments.
RewriteRule ^.*$ subfolder/public [NC,L]
由于 public
是文件系统上的物理目录,因此在内部重写该目录时,需要在其后加上斜杠.否则,mod_dir将尝试通过添加斜杠来修复" URL-这是外部重定向的来源.(mod_dir隐式触发从 subfolder/public
到 subfolder/public/
的外部重定向.)
Since public
is a physical directory on the file system, you need to include a trailing slash when internally rewriting to that directory. Otherwise, mod_dir is going to try to "fix" the URL by appending a slash - that is where the external redirect is coming from. (mod_dir implicitly triggers an external redirect from subfolder/public
to subfolder/public/
.)
因此,请在根 .htaccess
文件中尝试以下操作:
So, try the following instead in your root .htaccess
file:
RewriteRule .* subfolder/public/ [L]
重要的是斜线.不需要 RewriteRule
pattern 上的锚点( ^
和 $
),因为您要匹配一切.出于相同的原因,也不需要 NC
标志.
The important thing is the trailing slash. The anchors (^
and $
) on the RewriteRule
pattern are not required, since you are matching everything. And the NC
flag is also not required for the same reason.
一如既往,请确保在测试之前清除浏览器缓存.
As always, make sure the browser cache is clear before testing.
UPDATE#1:上面的单个指令将所有内容(包括静态资源)重写到目录 子文件夹/public/
,然后依靠子目录中的第二个 .htaccess
文件正确路由请求.为了正确地重写静态资源(在HTML中以相对于根的URL路径表示,格式为"/js/myjs.js"
),那么您将需要其他指令重写这些.
UPDATE#1: The single directive above rewrites everything, including static resources, to the directory subfolder/public/
which then relies on the second .htaccess
file in the subdirectory to correctly route the request. In order to allow static resources to be rewritten correctly (represented in the HTML as root-relative URL-paths, of the form "/js/myjs.js"
) then you will need additional directives in order to rewrite these.
例如,将所有 .js
和 .css
文件专门重写为/subfolder/public/...
中的实际位置
For example, to specifically rewrite all .js
and .css
files to the real location in /subfolder/public/...
# Rewrite static resources
RewriteRule (.+\.(?:js|css))$ subfolder/public/$1 [L]
# Rewrite everything else to the "public" directory
RewriteRule .* subfolder/public/ [L]
UPDATE#2:为了使上述内容更通用,并重写任何静态资源(图像,PDF,.txt等),我们可以进行检查对于重写之前文件的存在,例如:
UPDATE#2: To make the above more general, and to rewrite any static resource (images, PDFs, .txt, etc...) we can check for the existence of the file before rewriting, something like:
# Rewrite static resources
RewriteCond %{DOCUMENT_ROOT}/subfolder/public/$1 -f
RewriteRule (.+) subfolder/public/$1 [L]
# Rewrite everything else to the "public" directory
RewriteRule .* subfolder/public/ [L]
这意味着如果不存在任何 .css
,它将被传递到 subfolder/public/
.
This will mean that if any .css
does not exist it will be passed through to subfolder/public/
.
这篇关于htaccess-重定向到子文件夹而不更改浏览器URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!