2016-09-25 34 views
0

我一直在環顧網絡,特別是在這裏回答這個問題的計算器。如何從所有網址中刪除`/ 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

回答

0

您可以使用這些規則來滿足所有要求,包括刪除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> 
0

不要修剪尾部的斜線,你正在與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

相關問題