Apache LocationMatch everything but root

时间:2016-07-11 21:52:15

标签: regex apache pcre shibboleth locationmatch

I'm reading that <Location '/'> actually matches the entire domain, not just the root location. I want to create a Location or LocationMatch block that matches everything but http://my.domain.com/ This means it will trigger if any characters follow that final '/

Here is how I will be testing this:

<LocationMatch "REGEX GOES HERE">
    AuthType Shibboleth
    ShibRequireSession On
    Require Shibboleth
</LocationMatch>

I think Shibboleth may change some behavior. Also note I am using Apache 2.2 but a solution that works on 2.4 will suffice as well.

1 个答案:

答案 0 :(得分:0)

You can use LocationMatch with this regex:

<LocationMatch "^/.">

</LocationMatch>

Single DOT after ^/ will make sure there is at least one character after http://my.domain.com/ hence causing it to not to match landing page.

More details about LocationMatch

Testing:

Create this directive as:

<LocationMatch "^/(?<sitename>.+)">
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI}?u=%{env:MATCH_SITENAME} [L,R=302]
</LocationMatch>

Now to test I am doing this:

curl -kI -A "Chrome" -L 'http://localhost/index.php'
HTTP/1.1 302 Found
Date: Mon, 11 Jul 2016 22:31:37 GMT
Server: Apache/2.4.12 (Unix) OpenSSL/1.0.1j PHP/5.6.9 mod_wsgi/3.5 Python/2.7.9
Location: http://www.localhost/index.php?u=index.php
Content-Type: text/html; charset=iso-8859-1

HTTP/1.1 200 OK
Date: Mon, 11 Jul 2016 22:31:37 GMT
Server: Apache/2.4.12 (Unix) OpenSSL/1.0.1j PHP/5.6.9 mod_wsgi/3.5 Python/2.7.9
X-Powered-By: PHP/5.6.9
Content-Type: text/html; charset=UTF-8

curl -kI -A "Chrome" -L 'http://localhost/user.php'
HTTP/1.1 302 Found
Date: Mon, 11 Jul 2016 22:33:57 GMT
Server: Apache/2.4.12 (Unix) OpenSSL/1.0.1j PHP/5.6.9 mod_wsgi/3.5 Python/2.7.9
Location: http://www.localhost/user.php?u=user.php
Content-Type: text/html; charset=iso-8859-1

HTTP/1.1 200 OK
Date: Mon, 11 Jul 2016 22:33:57 GMT
Server: Apache/2.4.12 (Unix) OpenSSL/1.0.1j PHP/5.6.9 mod_wsgi/3.5 Python/2.7.9
X-Powered-By: PHP/5.6.9
Content-Type: text/html; charset=UTF-8

curl -kI -A "Chrome" -L 'http://localhost'
HTTP/1.1 200 OK
Date: Mon, 11 Jul 2016 22:32:47 GMT
Server: Apache/2.4.12 (Unix) OpenSSL/1.0.1j PHP/5.6.9 mod_wsgi/3.5 Python/2.7.9
X-Powered-By: PHP/5.6.9
Content-Type: text/html; charset=UTF-8

You can clearly see that www redirection doesn't happen when I request landing page but happens when I request /index.php