harelpls
harelpls

Reputation: 161

Regex match URL that does not contain strings but also contains other string

I am trying to match and replace asset URLs from a specific folder, without affecting other URLs in my .htaccess file. I have the .htaccess side of things down, am just struggling hardcore to match the correct URLs.

Given these three URLs:

I only want to match the last one (containing themes), and for that matter everything after /fonts/

current method matches everything:

RewriteRule /?fonts/(.*)$ wp-content/themes/ng-health/app/fonts/$1 [NC,L]

trying to use a negative lookahead, but doesn't work:

http:\/\/.+(?!wp-includes|plugins)\/fonts\/(.*) (matches everything: http://regexr.com/39vka)

Upvotes: 1

Views: 1105

Answers (2)

depsai
depsai

Reputation: 415

Try this also.

http:\/\/(?:(?!themes).)*themes\/fonts\/.*

SEE DEMO : http://regex101.com/r/fW1iC9/1

Upvotes: 0

nu11p01n73R
nu11p01n73R

Reputation: 26667

You were almost there.

  • Instead of using a negative look ahead use a negative look behind

Regex can be

http:\/\/.+(?<!wp-includes|plugins)\/fonts\/.*

Example : http://regex101.com/r/tE0dL9/1

Change made

  • (?<!wp-includes|plugins) negative look behind. Assertst that /fonts/ is not presceded by wp-includes or plugins

Upvotes: 1

Related Questions