2013-08-25 219 views
1

我試圖從.htaccess文件中使用此代碼(我承認我從這裏複製/粘貼其他問題)的網站的URL中刪除「.php」擴展名:「漂亮」的URL不工作

RewriteEngine on 
Options +FollowSymLinks  
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php 

.htaccess文件的其他行做的工作,比如,我有一個錯誤的重定向和:

RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC] 
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L] 

所以,我知道.htaccess文件是在服務一般。

我不知道這方面會出現什麼問題,所以我不確定從何處開始排除故障。有沒有人有指針?

在此先感謝。

+0

你的規則有什麼問題?人們仍然可以訪問您的.php文件?因爲你的規則是允許他們訪問你的網站,比如'domain.com/index',它可以在內部重定向到'index.php',但仍然允許直接訪問php。 – Prix

+0

對不起,我不清楚。我希望用戶能夠輸入,例如,「domain.com/page」,然後查看「domain.com/page.php」中實際存在的內容。基本上,我希望「.php」對用戶隱藏。但它不像我想的那樣工作。如果我輸入「domain.com/page」,則會出現404錯誤,而不是看到「domain.com/page.php」中的內容。感謝您的幫助。我對htaccess文件完全陌生,除了複製粘貼規則。我不知道從哪裏開始解決這個問題,或者我在這裏複製的規則甚至在語法上是否準確。 – leewaters

回答

1

鑑於您的域帳戶是/home/youraccount/public_html,你.htaccess將與以下內容public_html文件夾中:

Options +FollowSymLinks -MultiViews 

RewriteEngine on 
RewriteBase/

# First we redirect the www to non-www 
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC] 
RewriteRule ^/?(.*)$ http://domain.com/$1 [R=301,L] 

# now we redirect phpless URLs internally to the php 
# if folder does not exist 
RewriteCond %{REQUEST_FILENAME} !-d 
# but the file exists and ends with .php 
RewriteCond %{REQUEST_FILENAME}\.php -f 
# redirect to name.php 
RewriteRule ^([^/]+)/?$ $1.php [L] 

注:如果你有更多的規則,這可能會發生衝突,所以我不得不看在你的規則的其餘部分,但基本上上述應該按預期工作。

您將能夠同時訪問:

domain.com/index 

domain.com/index/ 

而且它會重定向到文件index.php

+0

這樣做。非常感謝你的幫助。你能說出我使用的代碼有什麼特別錯誤,或者它完全是錯誤的路徑嗎? – leewaters

+0

@leewaters好的一件事是,你正在使用'^(。*)$',它會嘗試將'domain.com/index /'與'domain.com/index/.php'匹配,這是行不通的由於結尾'/'除了它應該工作。在我的規則中,我所做的是,我得到的不是'/',並且最後一個'/'是可選的,這與使用'^(。*)/?$' – Prix