简体   繁体   English

正则表达式以匹配url中的所有.php文件扩展名,除非在admin部分中找到

[英]regex to match all .php file extensions in url except for if found in the admin section

I need to redirect all traffic on the live site to the same URL without the .php file extension, if the .php file extension is found. 如果找到了.php文件扩展名,我需要将实时站点上的所有流量重定向到不带.php文件扩展名的同一URL。 However, since this is a wordpress site, I need to exclude wp-login.php and any .php extension found in a url that has wp-admin/* within it. 但是,由于这是一个wordpress网站,因此我需要排除wp-login.php和在其中具有wp-admin / *的URL中找到的任何.php扩展名。

How can I accomplish this via a regex: 如何通过正则表达式完成此操作:

Currently, this matches all .php file extensions (.*).php /$1 当前,这匹配所有.php文件扩展名(.*).php /$1

But how to exclude wp-login.php and anytime the url contains wp-admin and ends with a .php extension? 但是,如何在网址包含wp-admin并以.php扩展名结尾的任何时候排除wp-login.php?

Here's what I've tried for excluding wp-login.php, but isn't working, just not sure how to do this: (\\/)(^[wp\\-login])?(.*).php /$1 这是我尝试排除wp-login.php的方法,但是没有用,只是不确定如何执行此操作: (\\/)(^[wp\\-login])?(.*).php /$1

How to do this properly? 如何正确执行此操作? And also exclude the wp-admin folder in the url with any strings that end with .php? 还要排除URL中带有以.php结尾的任何字符串的wp-admin文件夹吗?

For example: 例如:

/testing.php - Needs to Match 
/wp-login.php - Should not Match 
/wp-admin/edit-post.php - Should not Match 
/wp-admin/tools.php?page=testing.php - Should not Match 
/product/category/testing.php - Should Match

Here's the regex you need: 这是您需要的正则表达式:

^\/(?!wp-login|wp-admin).*\.php$

See the results here https://regex101.com/r/f4GWDB/2/ 在这里查看结果https://regex101.com/r/f4GWDB/2/

/testing.php   MATCH
/wp-login.php  X
/wp-admin/edit-post.php   X
/wp-admin/tools.php?page=testing.php   X
/product/category/testing.php   MATCH

You can use the following regex: 您可以使用以下正则表达式:

^\/((?!(wp-login|wp-admin)).*)\.php$

You can see the verification of the regex here 您可以在此处查看正则表达式的验证

/testing.php                           -  matches
/wp-login.php                          -  does not match
/wp-admin/edit-post.php                -  does not match
/wp-admin/tools.php?page=testing.php   -  does not match
/product/category/testing.php          -  matches

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM