Regex to replace file extension
I would like a regex to replace the .html file extension with a / so www.example.com/page.html becomes www.example.com/page/. I want to also exclude a folder from t开发者_开发技巧his rule so anything within www.example.com/specialdir/ should be excluded.
EDIT: This will be used in php
Better to use htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L,QSA]
Source: Snipplr
So it will change all http://somesite/someurl to http://somesite/someurl.html in server side.
Search for
(www\.example\.com/(?!specialdir/).*)\.html$
and replace all with \1
or $1
, depending on your regex implementation.
EDIT: In PHP:
$result = preg_replace('%(www\.example\.com/(?!specialdir/).*)\.html$%', '\1', $subject);
精彩评论