2016-08-23 162 views
0

我原本希望我的網頁可以使用和不使用文件擴展名。htaccess 301重定向到.html

這是我的工作htaccess的樣子:

RewriteEngine On 

# .php to .html 
RewriteCond %{THE_REQUEST} \ /(.+)\.php[?\s] [NC] 
RewriteRule^/%1.html [L,R=301] 

# don't need extension on the end 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.+?)/?$ $1.php [L] 

RewriteRule ^(.+)\.html/?$ $1.php [L,NC] 

我想逆轉上述並強制所有頁面解析爲.html。我想爲Google添加301個重定向。

回答

0
RewriteEngine on 
RewriteRule ^(.*)$ $1.php 

例如,這將正確改寫example.com/test作爲example.com/test.php

的請求可以通過下面的代碼

<IfModule mod_rewrite.c> 
Options +MultiViews 
RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.*)$ $1.php 
#RewriteRule ^([a-z]+)\/?$ $1.php [NC] 


RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule ^(.*)$ $1.html 
#RewriteRule ^([a-z]+)\/?$ $1.html [NC] 

</IfModule> 
刪除這兩個PHP HTML擴展