的.htaccess重写子文件夹 [英] .htaccess rewrite to subfolder

查看:186
本文介绍了的.htaccess重写子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:

在一个共享的Apache 2.2中的虚拟服务器,我已经尝试设置的.htaccess 来无缝地重新在<$的子文件夹present文件C $ C>的public_html 文件夹,它们的根。好像

In a virtual server on a shared Apache 2.2 box, I've attempted to configure .htaccess to seamlessly represent files in a subfolder of the public_html folder as if they are in the root.

也就是说,当用户访问 http://www.example.com/home ,我需要他或她能够无缝的(不重写URL在客户端浏览器)的看到 http://www.example.com/subfolder/home 的文件。

That is, when the user accesses http://www.example.com/home, I need him or her to seamlessly (without rewriting the URL in the client browser) see the files from http://www.example.com/subfolder/home.

我的的.htaccess 文件看起来是这样的:

My .htaccess file looks like this:

RewriteEngine on
RewriteRule !^subfolder/ subfolder%{REQUEST_URI}

的问题:

本作品,,但的重写它在客户端浏览器的URL。

This works, but it rewrites the URL in the client browser.

也就是说,用户将看到 http://www.example.com/subfolder/home/ 在他们的地址栏,但我需要他们看到的只是 http://www.example.com/home

That is, the user sees http://www.example.com/subfolder/home/ in their address bar, but I need them to see just http://www.example.com/home

我的理解是,我在的.htaccess 做了应该导致内部改写这是无形的最终用户,但现在看来,我得到一个外部重写。

My understanding is that what I've done in .htaccess should result in an "internal" rewrite that's invisible to the end user, but it appears that I'm getting an "external" rewrite.

我是什么做错了吗?

其他细节:

当用户访问 www.example.com ,它的确实的所希望的工作,在用户看到主页内容供应从的public_html /子文件夹/ default.html ,以及浏览器的地址栏中说 www.example.com

When the user accesses www.example.com, it does work as desired, in that the user sees home page content served up from public_html/subfolder/default.html, and the browser address bar says www.example.com.

然而,当用户点击联系我们的主页上的链接(codeD 的href =/接触),它会出现,但节目 www.example.com/subfolder/contact / 在浏览器地址栏,就好像做了一个内部​​重写的重定向代替。

However, when the user clicks the 'Contact' link on that home page (coded href="/contact"), it gets there, but shows www.example.com/subfolder/contact/ in the browser address bar, as if it has done a redirect instead of an internal rewrite.

如果我再删除地址栏中 /子文件夹文本和强制页面重新加载,它的工作原理 - 它加载正确的内容,并在地址栏遗体 www.example.com/contact /

If I then delete the /subfolder text in the address bar and force a reload of the page, it works--it loads the correct content, and the address bar remains www.example.com/contact/

实现注意事项:的情况下,它可以帮助,的public_html /子文件夹/联系人是的文件夹的包含 default.html 文件。

Implementation note: In case it helps, public_html/subfolder/contact is a folder containing a default.html file.

另一种看法:

如果我打开一个新的标签,并键入 www.example.com/contact / (注意结尾的斜杠)的,它按预期工作和期望的

If I open a new tab and type www.example.com/contact/ (note the trailing slash), it works as expected and desired.

但是,如果我打开一个新标签,然后键入 www.example.com/contact (没有尾随斜线)的,它重定向到 www.example.com/subfolder/contact / ,这是我不想要的东西。

But if I open a new tab and type www.example.com/contact (no trailing slash), it redirects to www.example.com/subfolder/contact/, which is what I don't want.

我觉得应该告诉我什么是错的,但得到的答复仍然逃避我。

I feel like that should tell me what's wrong, but the answer still eludes me.

推荐答案

为什么发生这种情况的原因是因为 mod_dir 。它有一个称为指令 DirectorySlash 重定向到它认为一个请求是一个目录和斜线缺少同一个URI以斜线附加的浏览器。问题是mod_dir和mod_rewrite的在网址/文件映射管道不同的地方得到应用。因此mod_rewrite的轧液的URI在一个点,那么当mod_dir做的事,改写URI被重定向所以在浏览器地址栏中的URL改变了,包括隐藏内部重写。

The reason why this is happening is because of mod_dir. It has a directive called DirectorySlash that redirects the browser when it thinks a request is made for a directory and the trailing slash is missing to the same URI with a trailing slash appended. The problem is mod_dir and mod_rewrite get applied at different places in the URL/file mapping pipeline. So mod_rewrite mangles the URI at one point, then when mod_dir does its thing, the rewritten URI gets redirected so the URL in the browser's address bar changes to include the "hidden" internal rewrite.

您可以做几件事情在这里。

You can do a couple of things here.

  1. 您可以强制尾随斜线自己的mod_rewrite,所以浏览器被重定向到后面的斜线的 的URI中达到mod_dir之前。这意味着所有的事情网址像 /家居 /联系人将包括斜线,如果一个人失踪,你重写code将重定向再加一。事情是这样的,之上的你当前的规则,以便它被首先应用:

  1. You can enforce trailing slashes yourself in mod_rewrite, so the browser gets redirected to include the trailing slash before the URI reaches mod_dir. This means all of your URLs for things like /home and /contact will include a trailing slash, if one is missing, your rewrite code will redirect and add one. Something like this, above your current rule so that it gets applied first:

RewriteCond %{REQUEST_URI} !^/subfolder
RewriteCond %{DOCUMENT_ROOT}/subfolder%{REQUEST_URI} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

  • 关闭 DirectorySlash 这样mod_dir停止这样做重定向。你可以简单地包括 DirectorySlash关在你的htaccess文件的顶部。然而,这里的缺点是一个可能的信息泄露的安全问题结尾的斜线丢失时

  • Turn off DirectorySlash so mod_dir stops doing this redirect. You can simply include DirectorySlash Off at the top of your htaccess file. However, the downside here is a possible information disclosure security issue when the trailing slash is missing.

    编辑:

    为了使用索引文件,没有一个斜线,你需要映射到它。试着改变你的规则:

    In order to use the index file, without a trailing slash, you need to map to it. Try changing your rules to:

        RewriteEngine On
        RewriteCond %{REQUEST_URI} !^/subfolder
        RewriteCond %{DOCUMENT_ROOT}/subfolder%{REQUEST_URI} -d
        RewriteRule ^(.*?)/?$ /subfolder/$1/ [L]
    
        RewriteCond %{REQUEST_URI} !^/subfolder
        RewriteRule ^(.*)$ /subfolder/$1 [L]
    

    这篇关于的.htaccess重写子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆