简体   繁体   English

如何使用.htaccess从cPanel服务器上的URL中删除文件夹

[英]How to remove folder from url on a cPanel server using .htaccess

I have a domain and an hosting that allows me to have as many subdomain as i need using cPanel. 我有一个域和一个主机,可以让我使用cPanel拥有尽可能多的子域。 I've created one called projects.test.com in which i upload all of my projects. 我创建了一个名为projects.test.com ,其中上传了所有项目。

projects.test.com points to this specific path: public_html/projects projects.test.com指向以下特定路径: public_html/projects

Now, my main domain, test.com , uses a 301 redirect to point to this path: public_html/home 现在,我的主域test.com使用301重定向指向此路径: public_html/home

and actually this bit is working, as if i type test.com , i'll be redirected to test.com/home . 实际上,这一点是有效的,就像我键入test.com ,我将被重定向到test.com/home

Now, i have already write a question ( here ) on how to remove the /home from the url, and i though it was working. 现在,我已经在( 这里 )写了一个关于如何从URL中删除/home的问题,尽管它正在工作。 The only issue i've got with that, is that for some reason adding 我唯一遇到的问题是由于某种原因添加

RewriteEngine on
RewriteBase /

RewriteRule ^$ home/ [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!home/).+)$ home/$1 [L,NC]

on my root .htaccess , it break the sub domain projects.test.com . 在我的根目录.htaccess ,它破坏了子域projects.test.com

So my question is, how can i change this to make it work only for the main domain without breaking this sub domain i currently have and others that i could create in future? 所以我的问题是,如何更改此设置以使其仅适用于主域,而又不破坏我当前拥有的该子域以及将来可以创建的其他子域?

And also, do i need to edit the root (intended as public_html folder, or the one one level above public_html ? Sorry but i'm new with all this type of server configuration and i'm still trying to learn. 而且,我是否需要编辑根目录(打算作为public_html文件夹,或位于public_html之上的一个级别?抱歉,但是我对所有这种服务器配置类型还是陌生的,我仍在尝试学习。

Thanks for any advice 感谢您的任何建议

EDIT 编辑

The .htaccess file in my root is this: root的.htaccess文件是这样的:

<IfModule mod_deflate.c>
    SetOutputFilter DEFLATE
    <IfModule mod_setenvif.c>
        # Netscape 4.x has some problems...
        BrowserMatch ^Mozilla/4 gzip-only-text/html

        # Netscape 4.06-4.08 have some more problems
        BrowserMatch ^Mozilla/4\.0[678] no-gzip

        # MSIE masquerades as Netscape, but it is fine
        # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

        # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
        # the above regex won't work. You can use the following
        # workaround to get the desired effect:
        BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

        # Don't compress images
        SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
    </IfModule>

    <IfModule mod_headers.c>
        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
</IfModule>

while the one in my public_html folder is: 而我的public_html文件夹中的那个是:

RewriteOptions inherit

RewriteEngine on
RewriteBase /

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

RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteRule ^/?$ "http\:\/\/www\.test\.com\/home" [R=301,L]

cPanel lets you put an addon or subdomain outside of (or above) the public_html directory. 通过cPanel,您可以将插件或子域放在public_html目录之外(或上方)。 Since you've already created the subdomain, login to cPanel and click on the Subdomains link. 由于已经创建了子域,因此登录cPanel并单击“ 子域”链接。 In the Modify a Subdomain section, you can edit the Document Root . 修改子域部分中,您可以编辑文档根目录 You'll need to do that and set the path to something like this: 您需要这样做并将路径设置为如下所示:

domains/projects.test.com

Doing this will keep all of your addon and subdomains organized in a single folder, outside of the public_html directory, and avoid a lot of headaches down the road. 这样做可以将所有插件和子域保持在public_html目录之外的单个文件夹中,并避免日后麻烦。 The next time you create an addon or subdomain, you will do the same thing and set the Document Root to domains/sub.test.com . 下次创建插件或子域时,将执行相同的操作,并将文档根目录设置为domains/sub.test.com

The absolute path to these addon and subdomains would be similar to this (except for the username and possibly the name of the home directory): 这些插件和子域的绝对路径类似于此(用户名和home目录的名称除外):

/home/<your_username>/domains/projects.test.com/

And, of course, your primary domain (the one associated with your account), will always point here: 当然,您的主域(与您的帐户相关联的域)将始终指向此处:

/home/<your_username>/public_html/

With that issue resolved, if you want all requests sent to home but you don't want it to show up in the URL, your .htaccess file (in your public_html directory) should look like this: 随着这个问题解决了,如果你想发送给所有的请求home ,但你希望它在URL中出现,你.htaccess文件(在你public_html目录)应该是这样的:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html
</IfModule>

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript

    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

    <IfModule mod_setenvif.c>
        SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
    </IfModule>

    <IfModule mod_headers.c>
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
</IfModule>

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options +FollowSymLinks -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteBase /

    RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
    RewriteRule .* - [L,R=405]

    RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
    RewriteRule ^/?$ http://www.test.com [R=301,L]

    RewriteCond %{HTTP_HOST} ^www\.test\.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/home/
    RewriteRule ^(.*)$ /home/$1 [L]
</IfModule>

You also mentioned that you have an .htaccess file in your root directory, which I assume you mean here: 您还提到了root目录中有一个.htaccess文件,我认为这是在这里:

/home/<your_username>/.htaccess

Apache (or whatever HTTP server your host runs) will not read that file. Apache(或主机运行的任何HTTP服务器)将不会读取该文件。 Only files in your public_html directory (or other document root directories) are read. 仅读取public_html目录(或其他文档根目录)中的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM