2014-01-07 122 views
1

我的.htaccess文件有問題。那麼,我試圖做兩件事情;刪除所有URL的擴展名「.php」,如:「localhost/about.php」爲「localhost/about」或「localhost/about /」。並重寫一個動態url:「localhost/user/index.php?usr = username」爲「localhost/user/username /」或「localhost/username」。重寫動態URL並刪除「.php」擴展名

我已經找到了一種方法來做這兩件事情。但是,如果我有刪除「.php」擴展名的代碼,則重寫動態url的代碼不起作用。如果我沒有代碼來刪除「.php」擴展名,則重寫動態url的代碼將起作用。

這是錯誤的網站給我,當我試圖打開個人資料頁:

未找到

請求的URL /用戶/安瑞科是不是此服務器上找到。

未找到

請求的URL /user/enric.php此服務器上找到。

我該怎麼辦?下面是.htaccess文件的代碼:

<IfModule mod_rewrite.c> 

Options -Multiviews 
Options +FollowSymLinks 
RewriteEngine On 
RewriteRule ^channels/page/([0-9]+)/?$ channels/index.php?p=$1 [L] 
RewriteRule ^channels/([_0-9a-z-]+)/?$ channels/index.php?o=$1 [L] 
RewriteRule ^channels/([_0-9a-z-]+)/([0-9]+)/?$ channels/index.php?o=$1&p=$2 [L] 

# Delete ".php" extension and adds "/" 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php 
RewriteRule (.*)\.php$ /$1/ [L,R=301] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.*)/$ $1.php [L] 

RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule .*[^/]$ $0/ [L,R=301] 

# User profiles 
RewriteRule ^user/([^/]*)/$ /user/index/?usr=$1 [L,R=301] 

</IfModule> 

溶液中,JON LIN

<IfModule mod_rewrite.c> 

Options -Multiviews 
Options +FollowSymLinks 
RewriteEngine On 
RewriteRule ^channels/page/([0-9]+)/?$ channels/index.php?p=$1 [L] 
RewriteRule ^channels/([_0-9a-z-]+)/?$ channels/index.php?o=$1 [L] 
RewriteRule ^channels/([_0-9a-z-]+)/([0-9]+)/?$ channels/index.php?o=$1&p=$2 [L] 

# User profiles 
RewriteRule ^user/([^/]*)/$ /user/index.php?usr=$1 [L] 

# Delete ".php" extension and adds "/" 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php 
RewriteRule (.*)\.php$ /$1/ [L,R=301] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} ^(.+)/$ 
RewriteCond %{DOCUMENT_ROOT}%1.php -f 
RewriteRule (.*)/$ $1.php [L] 

RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule .*[^/]$ $0/ [L,R=301] 

</IfModule> 

回答

0

您需要添加一個檢查,當你的.php擴展添加到一個請求,即PHP文件實際上存在,你應該移動你的用戶重寫之前你的php擴展的東西:

<IfModule mod_rewrite.c> 

Options -Multiviews 
Options +FollowSymLinks 
RewriteEngine On 
RewriteRule ^channels/page/([0-9]+)/?$ channels/index.php?p=$1 [L] 
RewriteRule ^channels/([_0-9a-z-]+)/?$ channels/index.php?o=$1 [L] 
RewriteRule ^channels/([_0-9a-z-]+)/([0-9]+)/?$ channels/index.php?o=$1&p=$2 [L] 

# User profiles 
RewriteRule ^user/([^/]*)/$ /user/index.php?usr=$1 [L] 

# Delete ".php" extension and adds "/" 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php 
RewriteRule (.*)\.php$ /$1/ [L,R=301] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} ^(.+)/$ 
RewriteCond %{DOCUMENT_ROOT}%1.php -f 
RewriteRule (.*)/$ $1.php [L] 

RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule .*[^/]$ $0/ [L,R=301] 

</IfModule> 
+0

令人驚歎!有用!非常感謝你Jon! –