开发者

Regex to replace file extension

开发者 https://www.devze.com 2023-04-08 19:53 出处:网络
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

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消