如何在 htaccess 中的 #ancors 和 ?queries 之前从 ulrs 中删除 *.php、index.php 和尾部斜杠

2023-12-02

我无法为我的问题找到令人满意的答案。已经上网冲浪三天了,但没有发现任何实际有效的东西。

我的网站结构如下:

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

由于我只能使用www or html or html_public在我的服务器中的目录中,我想将内容与所有 php 和用户不会看到或使用的其他内容分开。

我创建了一个名为/public并放置用户将看到的所有内容。但现在我想出于搜索引擎优化的原因美化网址。

我正在尝试删除 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 中,我设法将 www 重定向到非 www;制作/public将内容作为 root 并将其从 url 中隐藏;删除尾部斜杠(不在目录中,也不在 #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} ^[A-Z]{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

EDIT

我已经尝试过解决方案这个问题但它仍然给我 403 目录没有尾随斜杠加上 404 su 文件没有 php 扩展名!

另外如果我输入http://domain.com/public它会给我 403。如果我输入http://domain.com/public/它不会重定向到 root。

# 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 %{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]

您在这里要求的大部分内容都可以通过以下方式完成:

Options +MultiViews

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

出于所谓的“SEO 原因”或其他原因,尾部斜杠实际上并不是必需的。看https://webmasters.googleblog.com/2010/04/to-slash-or-not-to-slash.html了解更多详细信息,并节省您的时间和麻烦。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 htaccess 中的 #ancors 和 ?queries 之前从 ulrs 中删除 *.php、index.php 和尾部斜杠 的相关文章

随机推荐