我无法对我的问题找到一个令人满意的答案。已经过了三天的冲浪,却没有发现真正有效的东西。
我的网站结构如下:
·
|__data
|__controllers
|__helpers
|__partials
|__layouts
|__images
|__javascripts
|__stylesheets
|__public
|__index.php
|__subfolder
|__index.php
|__other-content.php由于我只允许在服务器中使用www、html或html_public目录,所以我希望将内容与所有的php和其他用户永远不会看到或使用的内容分离开来。
我创建了一个名为/public的目录,并将用户将看到的所有内容放入其中。但现在我想美化的网址SEO的原因。
我试图删除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内容作为根用户并将其隐藏在url中;删除尾随斜杠(不是在dir中,也不是在#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编辑
我已经尝试过来自this question的解决方案,但是它仍然给了我403在目录上没有尾随斜杠加上404个su文件,没有php扩展名!
另外,如果我输入http://domain.com/public,它会给我403号。如果我键入http://domain.com/public/,它将不会重定向到根。
# 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]发布于 2016-09-23 22:06:48
在关闭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,R=301,NE]
# remove trailing slash from all URLs
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+)/$ /$1 [R=301,L,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。
测试此更改时,请清除浏览器缓存。
发布于 2016-09-23 14:02:38
您在这里所要求的大部分内容都可以通过以下方式完成:
Options +MultiViews该指令的结果之一是,当请求资源时,例如/whatever MultiViews将通过扫描目录查找匹配的资源来透明地为/whatever.php服务。
尾随斜杠的东西实际上是不必要的,因为所谓的"SEO理由“或其他原因。有关更多细节,请参见https://webmasters.googleblog.com/2010/04/to-slash-or-not-to-slash.html,并为自己节省一些时间和麻烦。
https://stackoverflow.com/questions/39660361
复制相似问题