微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何删除* .php,index.php和从ulrs,#ancors和?htaccess查询中的尾部斜杠

我无法为我的问题find可以满足的答案。 这是三天的冲浪,没有发现实际的工作。

我的网站结构如下:

· |__data |__controllers |__helpers |__partials |__layouts |__images |__javascripts |__stylesheets |__public |__index.PHP |__subfolder |__index.PHP |__other-content.PHP

由于我被允许只在我的服务器中使用www或者html或者html_public目录,所以我想分离所有PHP和其他用户永远不会看到也不会使用的内容

我创build了一个名为/public的目录,并放置用户将看到的所有内容。 但现在我想美化search引擎优化的原因。

.htaccess重写正则expression式:匹配除“索引”

防止从Apache访问文件没有.htaccess

Htaccess维护模式允许某些目录

.htaccess RewriteRule添加到URL的驱动器path

包括不工作

我试图删除PHP和HTML扩展,删除index.PHP,并删除所有链接包括锚点,查询和目录的尾部斜杠。

例子:

http://www.domain.com/必须是http://domain.com/

http://domain.com/必须是http://domain.com

http://domain.com/public/必须是http://domain.com

http://domain.com/public/index.PHP必须是http://domain.com

http://domain.com/public/subfolder/必须是http://domain.com/subfolder

http://domain.com/public/subfolder/index.PHP必须是http://domain.com/subfolder

http://domain.com/public/subfolder/#anchor必须是http://domain.com/subfolder#ancor

http://domain.com/public/subfolder/?query必须是http://domain.com/subfolder?query

http://domain.com/public/subfolder/other-content.PHP必须是http://domain.com/subfolder/other-content

http://domain.com/public/subfolder/other-content.PHP#ancor必须是http://domain.com/subfolder/other-content#ancor

http://domain.com/public/subfolder/other-content.PHP?query必须是http://domain.com/subfolder/other-content?query

我实际上不使用查询,所以用这些方法来做这些并不重要,因为我应该对它们进行美化。

在我的htaccess我设法redirectwww到非www; 以/public内容为根目录并将其隐藏起来; 删除尾部的斜线(不是在dirs中,也不在#ancors中); 删除index.PHP

我试图强制从目录中删除尾部斜杠,但我总是得到一个403消息,我也试图删除扩展,但我得到一个404消息。

这是我的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 %{REQUEST_URI} !^/public RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ /public/$1 [NC,L,QSA] # Remove trailing slash RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^(.+)/$ /$1 [L,R=301] # Remove index.PHP RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.PHP?$1 [L,QSA] RewriteCond %{THE_REQUEST} ^[AZ]{3,}s(.*)/index.PHP [NC] RewriteRule ^ %1 [R=301,L] # Remove .PHP extention RewriteCond %{REQUEST_FILENAME}.PHP -f RewriteRule !.*.PHP$ %{REQUEST_FILENAME}.PHP [QSA,L] # Error pages ErrorDocument 403 /errors/403.PHP ErrorDocument 404 /errors/404.PHP ErrorDocument 500 /errors/500.PHP

编辑

我试过从这个问题的解决scheme,但它仍然给我403目录没有跟踪斜线加404文件没有PHP扩展!

另外如果我键入http://domain.com/public它会给我403.如果我键入http://domain.com/public/它不会redirect到根。

# 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,QSA] # remove trailing slash RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{THE_REQUEST} s(.+?)/+[?s] RewriteRule ^(.+?)/$ /$1 [R=301,L] # To internally forward /dir/file to /dir/file.PHP RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{DOCUMENT_ROOT}/$1.PHP -f [NC] RewriteRule ^(.+?)/?$ /$1.PHP [L]

移动PDF文件的htaccess规则?

用于漂亮URL的Apache Mod重写不起作用

我怎样才能禁用一个子类别的RewriteRule?

为什么我的子域名redirect到我的主域名?

301redirect和url连接

以这种方式关闭MultiViews :

Options +FollowSymLinks -MultiViews -Indexes DirectorySlash off RewriteEngine on RewriteBase / # Exceptions RewriteCond %{REQUEST_URI} ^/(images|javascripts|stylesheets)/ [NC] RewriteRule ^ - [L] # www to non-www RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC] RewriteCond %1##%{HTTPS}s ^(.+)##(?:on(s)|) RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301,NE] # Make /public like it was root RewriteCond %{THE_REQUEST} s/+public/(S*?)/?s [NC] RewriteRule ^ /%1 [L,NE] # remove trailing slash from all URLs RewriteCond %{THE_REQUEST} s(.+?)/+[?s] RewriteRule ^(.+)/$ /$1 [R=301,NE] # To externally redirect /dir/file.PHP to /dir/file RewriteCond %{THE_REQUEST} s/+(.+?).PHP[s?] [NC] RewriteRule ^ /%1 [R=301,NE,L] # Make /public like it was root RewriteCond %{REQUEST_URI} !^/public/ RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^.*$ public/$0 [L] # internally add trailing / to directories RewriteCond %{REQUEST_FILENAME} -d RewriteRule !/$ %{REQUEST_URI}/ [L] # To internally forward /dir/file to /dir/file.PHP RewriteCond %{DOCUMENT_ROOT}/$1.PHP -f [NC] RewriteRule ^(.+?)/?$ $1.PHP [L]

但请记住,对于Web服务器http://domain.com/与http://domain.com相同,Web服务器在这两种情况下都是相同的。 这是你的浏览器,在http://domain.com后添加/ 。

测试此更改时清除浏览器缓存。

在这里要求的大部分都可以用:

Options +MultiViews

该指令的结果之一是,当请求资源时,比如/whatever多视图通过扫描目录来查找匹配的资源,将透明地为/whatever.PHP服务。

对于所谓的“搜索引擎优化的原因”或其他方面,尾随的斜线实际上并不是必须的。 有关详情,请参阅https://webmasters.googleblog.com/2010/04/to-slash-or-not-to-slash.html ,并节省一些时间和麻烦。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐