使用 .htaccess 将 Web 根文件夹设为子文件夹? [英] Make web root folder a sub-folder with .htaccess?

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

问题描述

我正在处理一个非常复杂的项目,所以我想使用一个有意义的文件/文件夹结构.

I'm working on a pretty complex project, so I'd like to go with a file/folder structure that makes sense.

我想要的文件夹/文件结构是:

.htaccess
/php/
/assets/

我想要人们可以访问的网页:

/php/views/pages/

我想知道是否可以仅使用 .htaccess/php/views/pages 设置为可查看的根",例如,当人们访问 http://mydomain.com/ 他们将查看 http://mydomain.com/php/views/pages/index.php,如果是这样,我该怎么做?

I'm wondering if it's possible to use just .htaccess to set /php/views/pages as the "viewable root", so that for example, when people visit http://mydomain.com/ they'll be viewing http://mydomain.com/php/views/pages/index.php, and if so, how would I go about doing it?

此外,是否可以使用 .htaccess 和 301 重定向来规范对 /php/views/pages 的所有访问,以阻止多个链接在搜索引擎中被索引?

Also, is it possible to canonicalize all access to /php/views/pages using .htaccess and a 301 redirect to stop multiple links being indexed in search engines?

推荐答案

如果您无权访问您的主机配置

DocumentRoot 只能用于服务器和虚拟主机配置,必须是 .htaccess.

If you don't have access to your host configuration

DocumentRoot can only be used in server and virtual host configs, .htaccess it must be.

  1. 让我们在您的 .htaccess 中添加指令

  1. Let's add directives in your .htaccess

RewriteEngine on

  • 我假设您想让对/assets 的请求通过

  • I assume you want to let the requests to /assets go through

    #if a match for asset is found, do nothing
    RewriteRule ^assets/ - [L]
    

  • 如果请求尝试直接访问 /php/views/pages/,请将其重定向到其规范版本

  • If a request tries to access /php/views/pages/ directly, redirect it to its canonical version

    RewriteCond %{THE_REQUEST} php/views/pages/
    RewriteRule ^php/views/pages/(.*) http://mydomain.com/$1 [R=301,L]
    

  • 并添加规则以将其他所有内容映射到 /php/views/pages/

    RewriteCond %{REQUEST_URI} !php/views/pages/
    RewriteRule ^(.*)$ /php/views/pages/$1 [L]
    

  • 如果您有权访问您的主机配置

    忘记 .htaccess 文件并使用它.示例配置可能看起来像

    If you have access to your host configuration

    Forget about .htaccess files and use it. A sample configuration could look like

    # ~base stands for your original DocumentRoot
    <VirtualHost *:80>
        DocumentRoot ~base/php/views/pages
        Alias /assets ~base/assets
    
        # other configuration parameters
    </VirtualHost>
    

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

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