【发布时间】:2017-02-01 15:31:59
【问题描述】:
感谢this answer 和this answer,我已经设法从目录中“重新设置”根目录,以便将公共文件与配置、帮助程序和控制器分开。
但是我意识到,如果用户输入 http://domain.com/public,它不会重定向到 http://domain.com,这对 SEO 是有害的,因为机器人会将它们视为两个 url,因此会重复。
我需要 301 重定向 /public 到 root 但只要我在尝试。它不起作用。
我的 htaccess 如下所示:
# 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//
我该如何解决这个问题?
编辑
我的网站目录结构是:
·
|__data
|__controllers
|__helpers
|__partials
|__layouts
|__images
|__javascripts
|__stylesheets
|__public
|__index.php
|__subfolder
|__index.php
在根目录下不会有任何 index.php 页面。所以我需要创建/public 目录,因为它是我的根目录,以便将用户不必看到的配置和其他目录分开。
【问题讨论】:
-
public/中是否也有 .htaccess 文件?还有什么不适合你? -
@anubhava 在
/public中只有像 url 一样工作的公共文件。/public的子目录也可用作链接。该文件夹内没有.htaccess。它不起作用的是,如果用户键入http://domain.com和http://domain.com/public/,浏览器将显示相同的页面。错误是/public以这种方式创建了两个链接,这将为 Google 生成重复项。我需要删除网址中的public,就像它是由斜杠和扩展名组成的一样
标签: apache .htaccess redirect mod-rewrite