如何从所有网址中删除`/ index.php`,包括最后的斜线?

问题描述:

我一直在环顾网络,特别是在这里回答这个问题的计算器。如何从所有网址中删除`/ index.php`,包括最后的斜线?

我只使用目录,其中index.php文件。可包含的内容和私人内容在public_htmlcpanel)目录之外。

通过使用目录,锚链接和查询看起来像:

http://domain.com/sub/#anchorhttp://domain.com/sub/?query

而我只是不喜欢它。人们总是告诉我,这与搜索引擎优化无关,但我不认为这是事实。首先,因为我想要一致性,其次,尾部斜杠创建重复因此,我需要301重定向!一致性和重复确实是一个SEO问题!

这只是我的网站是如何构成的一个例子:

/ 
·--index.php 
| 
·--/about-us/ 
| | 
| ·--index.php 
·--/contact-us/ 
    | 
    ·--index.php 

用户永远不会知道about-us是一个目录,他们不会键入反正最后结尾的斜线。这会造成重复和HTTP错误。

在网络上,我发现只有非工作的例子,至于我的理解,我必须内部添加尾部斜杠和index.php。这有助于避免安全问题,并使所有的事情工作!

Herehere我问过类似的东西。但在这种情况下,我不得不创建/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

+0

'http://domain.com/R-> http:// domain.com'应该已经在浏览器处理了。 – anubhava

+0

另一点,锚点不能被处理服务器端,因为服务器甚至不会在HTTP请求中获得'#anchor',即在Apache日志中只会接收到'http:// domain.com/sub /'。 – anubhava

+0

如何删除'/ index.php'? – dcdeiv

您可以使用这些规则来满足所有要求,包括删除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/foohttp://domain.com/foo/对SEO有好处。您的网址中的index.php字符串是如何排在第一位的?他们不需要在那里;确保你所有的链接都是免费的。确保唯一页面索引的最简单方法是使用<link rel=canonical href="…">

+0

所以你建议不要删除结尾斜杠和301重定向非削减链接到斜杠链接? – dcdeiv

+0

是的,这就是Apache已经想做的事情,尽管检查开发工具窗口的网络选项卡以确保它们是301s而不是302s。不要担心其他网站与'index.php'中你的链接;随着时间的推移它们会消失。只要*你*避免使用它们并使用规范,你就会有很好的搜索引擎结果,并且实际上减少了重定向的机会,这对于性能更好。 – Walf

+0

您甚至可以更改Apache的'DirectorySlash'配置以避免重定向性能下降,但请注意文档中的安全说明。 – Walf