我无法为我的问题找到令人满意的答案。已经上网冲浪三天了,但没有发现任何实际有效的东西。
我的网站结构如下:
·
|__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]