2014-08-29 69 views
1

我有一個子域作爲一個全新的網站(subdomain.domain.com = subdomain.com)。我主要使用.html文件,但希望擺脫這些擴展名(而不是http://subdomain.com/file.html,我想要http://subdomain.com/file)。我已經使用下面的.htaccess代碼來實現這一點。使用.htaccess刪除多個文件擴展名

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ $1.html [L,QSA] 

唯一的問題是,我也需要擺脫網站上其他文件的.php擴展名。大多數人說用.php替換.html來複制上面的代碼。我已經做到了;不起作用。我已經嘗試了許多其他版本的代碼,但仍然無法正常工作。

因爲我不是專家,使用.htaccess,任何人都可以請解釋如何刪除.html和.php文件擴展名? 在此先感謝。

我不認爲這是一個重複的問題,因爲我沒有看到任何人問如何一次替換多個文件擴展名。

回答

2

您可以使用重寫規則是這樣的:

Options +FollowSymLinks 
RewriteEngine on 

# for adding .html extension if matching file exists 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC] 
RewriteRule ^(.+?)/?$ /$1.html [L] 

# for adding .php extension if matching file exists 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC] 
RewriteRule ^(.+?)/?$ /$1.php [L] 
+0

感謝您的回答;它會拋出一個404雖然 – 2014-08-29 14:40:56

+0

該文件位於主域文件夾(不是子域)。而且你的代碼會拋出404無論其他規則是否存在 - 我將它們全部刪除並嘗試了它... – 2014-08-29 14:54:37

+0

也使它成爲'RewriteRule ^(。+?)/?$ $ 1.html [L]'(或者更新) – anubhava 2014-08-29 14:56:53

0

要刪除多個文件擴展名,您可以在/root/.htaccess使用以下規則。

RewriteEngine on 

RewriteBase/
#terminate all rewrite loops 
RewriteCond %{ENV:REDIRECT_STATUS} 200 
RewriteRule^- [L] 
#1)Remove ".html" , ".php",".css" extension. 
#The following rule redirects "/file.ext" to "/file" 
RewriteRule ^(.+)\.(html|php|css)$ $1 [L,R,NE] 
#checks if "/file" .html exists, if it exits ,map "/file" to "/file.html" 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule ^(.*?)/?$ $1.html [L] 
#checks if "/file" .php exists, if it exits ,map "/file" to "/file.php" 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.*?)/?$ $1.php [L] 
#checks if "/file" .css exists, if it exits ,map "/file" to "/file.css" 
RewriteCond %{REQUEST_FILENAME}.css -f 
RewriteRule ^(.*?)/?$ $1.css [L] 

上述規則刪除.php,.html和.css擴展名。要刪除更多擴展名,您可以根據需要編輯.htaccess。

相關問題