Search code examples
regexapacheapache-config

Skip one string in regex


I have the following apache rewrite rule:

000-default.conf:    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

How would I change this to allow everything except if the following string is in the url: tab=availability ?


Solution

  • You can use negation in RewriteRule itself:

    RewriteRule !tab=availability https://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R]
    

    This will redirect all the requests with URI not containing given pattern. However if you are using above rule in .htaccess then better to add

    RewriteCond %{HTTPS} off
    

    to avoid redirect loop.

    Moreover if you want to avoid matching given pattern anywhere in URL including QUERY_STRING then use THE_REQUEST variable in your condition:

    RewriteCond %{HTTPS} off
    RewriteCond %{THE_REQUEST} !tab=availability [NC]
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R]