0

I'm working on an apache server (2.2), and I'm trying to redirect a URL based off of a URL filter. For example,

 https://mywebsite.com/path/to/page?folder=folderDirectory/folderName

will redirect to:

 https://mywebsite.com/static/contentUnavailable.html

In my httpd.conf file I have the following code ..

 RedirectMatch (.*)path/to/page?folder=folderDirectory/folderName /static/contentUnavailable.html

I restart apache everytime I make modifications to this file, however the page is not redirecting. What am I doing wrong in the RedirectMatch?

1 Answer 1

1

You can't match query string with a redirectmatch, sorry, you need mod_rewrite for this and using a RewriteCond. Rough example:

RewriteCond %{QUERY_STRING} ^folder
RewriteRule ^ /static/contentUnavailable.html [R,L,QSD]

This will match a query string that starts with folder (and continues with whatever else, no matter what it is). and redirect everything to the destination you want, discarding the query string in the process (QSD flag).

In any case let me commend you for trying to stick to redirect/redirectmatch first (while everyone else just goes blindly for mod_rewrite even for the simplest redirects). You are doing things right.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for the response @ezra-s ! I tried the above code and a bunch of other things but no luck. I also tried putting it within a <directory /> block but that also didn't work. Does this have to be in .htaccess instead of httpd.conf? I tried both locations but nothing seems to work.
This will work in any context, but no need for .htaccess, specially since you have access to the main configuration files. Stick to virtualhost context as much as possible, it will make things simpler. When you say "don't work" what happens instead? Try to be as descriptive as possible since the rules above "do work" (I tested them) so maybe something else is interfering (other rewrites or virtualhost, etc..)
Gotcha! So I just tried to match the URI and it does indeed pick up on that within the httpd.conf file and I am redirected. For example, if my URL is example.com/path/to/page?folder=foo, and I edit the query string to match 'path/to/page', it redirects me to /static/contentUnavailable.html. However, if I specifically try to match 'foo' for the query string, it does not redirect me, it takes me to the webpage with that specific folder content - almost as if its saying that nothing in the URL matches 'foo'. I've tried other webpages as well, but querying the filter isn't working for me.
Ah, I found the problem .. Your code does indeed work. When I match query string to 'folder' it redirects me.. however I need to redirect on what the folder is actually equivalent too ... so for example if my URL is example.com/path/to/page?folder=foo, I need to redirect on 'foo' or if URL is example.com/path/to/page?folder=bar, I need to redirect on 'bar'. I have a set of folders that I need to redirect, but some folders that I do not need to redirect.
You can easily match bits of the query string like RewriteCond %{QUERY_STRING} ^folder=(.*) and then use %1 in the rewriterule destination where you want that captured bit to go. You capture with parenthesis. Apache HTTPD uses PCRE (Perl Compatible Regular Expressions).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.