如何从所有网址中删除`/ index.php`,包括最后的斜线?
我一直在环顾网络,特别是在这里回答这个问题的计算器。如何从所有网址中删除`/ index.php`,包括最后的斜线?
我只使用目录,其中index.php
文件。可包含的内容和私人内容在public_html
(cpanel)目录之外。
通过使用目录,锚链接和查询看起来像:
http://domain.com/sub/#anchor
或http://domain.com/sub/?query
而我只是不喜欢它。人们总是告诉我,这与搜索引擎优化无关,但我不认为这是事实。首先,因为我想要一致性,其次,尾部斜杠创建重复因此,我需要301重定向!一致性和重复确实是一个SEO问题!
这只是我的网站是如何构成的一个例子:
/
·--index.php
|
·--/about-us/
| |
| ·--index.php
·--/contact-us/
|
·--index.php
用户永远不会知道about-us
是一个目录,他们不会键入反正最后结尾的斜线。这会造成重复和HTTP错误。
在网络上,我发现只有非工作的例子,至于我的理解,我必须内部添加尾部斜杠和index.php
。这有助于避免安全问题,并使所有的事情工作!
Here和here我问过类似的东西。但在这种情况下,我不得不创建/public
目录。现在我正在管理更改主机,我将能够使用非公共目录来存储php文件。
由于我不再需要/public
目录,我复制了部分代码并将其粘贴到新的.htaccess
上。
这里是.htaccess
# Options
Options +FollowSymLinks -MultiViews -Indexes
DirectoryIndex index.php index.html
DirectorySlash off
# Enable Rewrite Engine
RewriteEngine on
RewriteBase/
# 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]
# 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]
# 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]
<FilesMatch ^\.>
order allow,deny
deny from all
</FilesMatch>
<Files *.inc>
order allow,deny
deny from all
</Files>
代码似乎子目录中而不是在根很好地工作。我想这是因为上面的代码是为了在/public
子目录上工作而定制的。
我怎样才能让它在根上工作呢?
总结,我需要301重定向,否则会有重复的内容!
http://www.domain.com/
的R - >http://domain.com/
http://domain.com/
的R - >http://domain.com
http://domain.com/sub/
的R - >http://domain.com/sub
http://domain.com/sub/index.php
的R - >http://domain.com/sub
http://domain.com/sub/index.php#anchor
的R - >http://domain.com/sub#anchor
http://domain.com/sub/#anchor
R - >http://domain.com/sub#anchor
您可以使用这些规则来满足所有要求,包括删除index.php
。请记住,在服务器端无法保留锚点,因为服务器在HTTP请求中甚至不会得到#anchor
,即在Apache日志中只会收到http://domain.com/sub/
。
# Options
Options +FollowSymLinks -MultiViews -Indexes
DirectoryIndex index.php index.html
DirectorySlash off
# Enable Rewrite Engine
RewriteEngine on
RewriteBase/
# 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]
# remove index.php
RewriteCond %{THE_REQUEST} \s/*(/.*)?/index\.php[?\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]
# 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]
<FilesMatch ^\.>
order allow,deny
deny from all
</FilesMatch>
<Files *.inc>
order allow,deny
deny from all
</Files>
不要修剪尾部的斜线,你正在与URL的正常目录结构作斗争。即使您的浏览器隐藏了该网址,也没有这样的网址,斜杠仍然在请求中发送,并且会被添加回从地址栏复制的网址。
为单个域做重定向很好。自动301重定向(Apache所做的那种)从http://domain.com/foo
到http://domain.com/foo/
对SEO有好处。您的网址中的index.php
字符串是如何排在第一位的?他们不需要在那里;确保你所有的链接都是免费的。确保唯一页面索引的最简单方法是使用<link rel=canonical href="…">
。
'http://domain.com/R-> http:// domain.com'应该已经在浏览器处理了。 – anubhava
另一点,锚点不能被处理服务器端,因为服务器甚至不会在HTTP请求中获得'#anchor',即在Apache日志中只会接收到'http:// domain.com/sub /'。 – anubhava
如何删除'/ index.php'? – dcdeiv