从包含短划线的文件名中删除.php扩展名

时间:2016-08-01 03:47:02

标签: .htaccess mod-rewrite url-rewriting url-redirection friendly-url

用于删除.php扩展名表单网址,其中包含短划线,如

http://www.sitename.com/here-the-filename-with-dashes.php
http://www.sitename.com/filename.php
to
http://www.sitename.com/here-the-filename-with-dashes
http://www.sitename.com/filename

我已经尝试了几个代码,但没有通过htaccess文件使用。 这里是HTACCESS FILE:

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f   
#RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php
#RewriteRule ^(.*?)/?$ $1.php 
RewriteRule ^(.*)$ $1.php
#RewriteRule ^(.*)$http://www.sitename.ca/?$1.php [L,R=302]
#RewriteRule ^([^\.]+)$ $1.php [NC,L]
#RewriteRule ^([a-z]+)\/?$ $1.php [NC]
#RewriteRule ^([^/]+)/$ $1.php 

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

1 个答案:

答案 0 :(得分:0)

你走在正确的道路上。

Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteCond %{HTTP_HOST} ^sitename.com$ [NC] 
RewriteRule ^ http://www.sitename.com%{REQUEST_URI} [L,R=301] 

RewriteRule ^index.php$ http://www.sitename.com/ [R=301,L]

RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php [NC]
RewriteRule ^ /%1 [QSA,R=301,L]

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