将子文件夹重定向到root并将其隐藏在url中 [英] Redirect subfolder to root and hide it in the url

查看:94
本文介绍了将子文件夹重定向到root并将其隐藏在url中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于此答案.

但是我意识到,如果用户键入http://domain.com/public,它将不会重定向到http://domain.com,这对SEO有害,因为机器人会将它们视为两个重复的网址.

However I realized that if a user type http://domain.com/public it will not redirect to http://domain.com this is harmful for SEO because robots will treat them as two urls thus duplicate.

我需要301将/public重定向到root,但是只要我在尝试.这是行不通的.

I need to 301 redirect /public to root but as long as I am trying. It doesn't work.

我的htaccess如下所示:

My htaccess looks like the following:

# Options
Options +FollowSymLinks +MultiViews -Indexes
DirectorySlash off

# Enable Rewrite Engine
RewriteEngine on
RewriteBase /

# Exceptions
RewriteCond %{REQUEST_URI} ^/(images|javascripts|stylesheets)/ [NC]
RewriteRule ^ - [L]

# www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301]

# Make /public like it was root
RewriteCond %{THE_REQUEST} /public/([^\s]+) [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_URI} !^/public
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/$1 [NC,L,QSA]

# Error pages
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php

<files .htaccess="">
  order allow,deny
  deny from all
</files>

通过这种方式,它将重定向到http://domain.com//

In this way it will redirect to http://domain.com//

如何解决此问题?

编辑

我的网站目录结构是:

·
|__data
|__controllers
|__helpers
|__partials
|__layouts
|__images
|__javascripts
|__stylesheets
|__public
   |__index.php
   |__subfolder
      |__index.php

在根目录下,不会有任何index.php页面.因此,我需要将/public目录作为我的根目录,以便将用户不必看到的配置和其他目录分开.

In root there won't be any index.php pages. So I need to make the /public dir as it was my root, in order to keep configs and other directories that users don't have to see separate.

推荐答案

将2条与public/相关的规则替换为此:

Replace your 2 public/ related rules to this:

# add a trailing slash if public/$1 is a directory
RewriteCond %{DOCUMENT_ROOT}/public/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=301,NE]

# Make /public like it was root
RewriteCond %{THE_REQUEST} \s/+public/(\S*)\s [NC]
RewriteRule ^ /%1 [L,R=301,NE]

# internally add public/ to URIs
RewriteRule ^(?!public/).*$ public/$0 [L,NC]

如果您将URL用于子目录,例如,没有第一个尾部斜杠添加规则. http://domain.com/subfolder,然后在第三条规则中在URI中添加public/后,其内部将变为http://domain.com/public/subfolder,这是一个有效目录,Apache的mod_dir模块最后添加一个斜杠,并进行301重定向到http://domain.com/public/subfolder/,这将暴露 public/部分给您的客户.

Without first trailing slash add rule if you use a URL for sub-directory e.g. http://domain.com/subfolder then after 3rd rule adding public/ in the URI it internally becomes http://domain.com/public/subfolder which is a valid directory and Apache's mod_dir module adds a trailing slash in the end doing a 301 redirect to http://domain.com/public/subfolder/ and this will expose public/ part to your clients.

当有第一个斜杠时,我们检查目标位置是否为目录,并在原始 /puclic/ URL本身中添加一个斜杠.

When first trailing slash is there we check if destination is directory and add a trailing slash in original non-/puclic/ URL itself.

这篇关于将子文件夹重定向到root并将其隐藏在url中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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