问题描述
我无法找到我的问题的令人满意的答案.冲浪已经三天了,没有发现任何有效的方法.
我的网站结构如下:
·
|__data
|__controllers
|__helpers
|__partials
|__layouts
|__images
|__javascripts
|__stylesheets
|__public
|__index.php
|__subfolder
|__index.php
|__other-content.php
由于只允许在服务器中使用www
或html
或html_public
目录,因此我希望将内容与所有php和用户永远不会看到或使用的其他内容分开. /p>
我创建了一个名为/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中隐藏它;删除尾部的斜杠(不在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} ^[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
编辑
我已经尝试过这个问题的解决方案,但它仍能在目录上显示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]
以这种方式关闭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
之后添加了/
.
在测试此更改时清除浏览器缓存.
I am unable to find a satisfiable answer to my problem. It's been three days of surfing and finding nothing that actually works.
My site structure is the following:
·
|__data
|__controllers
|__helpers
|__partials
|__layouts
|__images
|__javascripts
|__stylesheets
|__public
|__index.php
|__subfolder
|__index.php
|__other-content.php
Since I am allowed to only use the www
or html
or html_public
directory in my server, I want to separate content from all the php and other stuff that users won't ever see nor use.
I created a directory called /public
and put all content the user will see. But now I want to prettify urls for SEO reasons.
I am trying to remove the php and html extention, to remove index.php and to remove the trailing slash from all links including anchors, queries and directories.
Examples:
http://www.domain.com/
must be http://domain.com/
http://domain.com/
must be http://domain.com
http://domain.com/public/
must be http://domain.com
http://domain.com/public/index.php
must be http://domain.com
http://domain.com/public/subfolder/
must be http://domain.com/subfolder
http://domain.com/public/subfolder/index.php
must be http://domain.com/subfolder
http://domain.com/public/subfolder/#anchor
must be http://domain.com/subfolder#ancor
http://domain.com/public/subfolder/?query
must be http://domain.com/subfolder?query
http://domain.com/public/subfolder/other-content.php
must be http://domain.com/subfolder/other-content
http://domain.com/public/subfolder/other-content.php#ancor
must be http://domain.com/subfolder/other-content#ancor
http://domain.com/public/subfolder/other-content.php?query
must be http://domain.com/subfolder/other-content?query
I actually don't use queries yet so it is not important to make them be in those ways, since I should prettify them...
In my htaccess I managed to redirect www to non-www; make /public
content as root and hide it from url; remove trailing slash (not in dirs, nor in #ancors); remove index.php
I am trying to force removing the trailing slash from directories but I always get a 403 message, I am also trying to remove the extention but I get a 404 message.
Here is my 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
I have tried the solution from this question but it still give me 403 on directories without trailing slash plus 404 su files without the php extension!
Plus if I type http://domain.com/public
it will give me 403. If I type http://domain.com/public/
it will not redirect to 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]
Have it this way with MultiViews
turned off:
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]
But keep in mind that for a web server http://domain.com/
is same as http://domain.com
and web server gets /
in both cases. It is your browser that adds a /
after http://domain.com
.
Clear your browser cache when testing this change.
这篇关于如何在#ancors和htaccess中的查询之前从ulrs中删除* .php,index.php和尾部斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!