LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2019, 03:58 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,158

Rep: Reputation: 87
redirectmatch negative lookahead regex


Hi,

I came across the folllowing redirect match for Apache on the following site: https://www.hyperborea.org/journal/2...-lets-encrypt/

Code:
RedirectMatch 301 ^(?!/\.well-known/acme-challenge/).* https://example.com$0
If I'm not mistaken, this is a negative lookahead regex, right? The question is, why would someone need something so complex in this case?
Or wouldn't it work otherwise with a RedirectMatch?
 
Old 05-23-2019, 01:47 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,434
Blog Entries: 11

Rep: Reputation: 3398Reputation: 3398Reputation: 3398Reputation: 3398Reputation: 3398Reputation: 3398Reputation: 3398Reputation: 3398Reputation: 3398Reputation: 3398Reputation: 3398
I am not sure why you think it is "so complex"...?

It simply says: Serve requests for anything below /.well-known/acme-challenge/ from this host, redirect everything else to https://example.com/

Assuming the host where this is located is the http://example.com host, this would allow LetsEncrypt authentication requests to be served here as required while redirecting everything else to the secure host.

As to why they did it this way rather than another, you would have to ask them (I have not visited the link), but probably because it is so simple.

Last edited by astrogeek; 05-23-2019 at 01:52 PM. Reason: kill links, tpoys
 
Old 05-23-2019, 04:08 PM   #3
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,158

Original Poster
Rep: Reputation: 87
Well, it's not so complex after all, but I guess you'd use lookahead and lookbehind regex in particular situations, and that is to say, when you try to match only a certain regex, and NOT the regex that it is bordering with. So you'd use it as a border.
Wheras in this situation, I'd have simply used a NOT match for the whole expression. This is saying match only ^ and whatever follows it with the exception of /.well-known/acme-challenge, but in this case I don't see any use in separating ^ from the rest. You'd normally use a ! for the whole thing, I guess.

Last edited by vincix; 05-23-2019 at 04:09 PM.
 
Old 08-03-2020, 08:48 AM   #4
DocRoot
LQ Newbie
 
Registered: Aug 2020
Posts: 2

Rep: Reputation: Disabled
Quote:
Originally Posted by vincix View Post
Wheras in this situation, I'd have simply used a NOT match for the whole expression. .... You'd normally use a ! for the whole thing, I guess.
Except that the mod_alias RedirectMatch directive does not support negated matches (with a ! prefix), so if you want to express a regex that does not match you need to use a negative lookahead.

To use the negated regex (!) prefix you need to use the (marginally more expensive) mod_rewrite RewriteRule (and/or RewriteCond) directive.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
LXer: Finding Overlapping Matches Using Perl's Lookahead Assertion Matching On Linux LXer Syndicated Linux News 0 09-09-2008 08:11 AM
Trying to combine lookahead and variable substitution djeepp Programming 1 09-03-2008 04:32 PM
Apache RedirectMatch Directive sir-lancealot Linux - Server 1 01-24-2008 04:14 AM
RedirectMatch & ScriptAlias richwalker Fedora 0 07-10-2005 06:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration