2016-04-15 135 views
1

我需要301將所有以.php結尾的URL重定向到非擴展版本。但重寫規則需要與現有的重寫規則配合使用。htaccess從多個正斜槓的URL中刪除.php擴展

# ==== REWRITE URLS ==== 
RewriteEngine On 
# pass through root 
RewriteRule ^(index\.php|Sitemap\.xml)?$ - [L] 
# no more/so add extension 
RewriteCond $1 !/ 
RewriteCond $1 !\.php$ 
RewriteCond ${REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ /$1.php [L] 
RewriteCond %{REQUEST_URI} !\.php$ 
RewriteRule ^(_assets|_css|_fonts|_includes|_scripts)($|/) - [L] #exclude these folders using the 'last: L' flag 
RewriteRule ^(.*)/(.*)$ /$1-$2 [L] 

這是此題的延伸here

所以期望的結果是:

domain.com/region/brand/more-content-here.php 

永久重定向到:

domain.com/region/brand/more-content-here 

但獲取實際的文件在:

domain.com/region-brand-more-content-here.php 

嘗試了一些不同的想法,但他們沒似乎不符合現有規則,而htaccess並不是我的力量。也需要它是查詢字符串友好。任何幫助,將不勝感激。

回答

1

要刪除PHP擴展,你可以使用:

RewriteEngine on 

#1)Permanently redirect "foo.php" to "/foo"# 
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC] 
RewriteRule^/%1 [NE,L,R=301] 
#2)Rewrite "/foo" to "/foo.php"# 
#this will internally redirect file to file.php# 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule^%{REQUEST_URI}.php [L] 
+1

完美!謝謝。我所需要的只是第一步,因爲#2已經在現有規則中涵蓋了。 – Dodo

1

工作代碼:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.php -f 
相關問題