1

I have a series of urls that look like this:

example.com/something/page
example.com/another

that I need to redirect to

example.com/folder/to/file?q=something/page
example.com/folder/to/file?q=another

I'm not so sure where to put the %{QUERY_STRING} (or if it's needed)? Thanks in advance!

2
  • Its safer to use example.com for examples. That way, you don't accidentally hit a real domain. I have changed it. And I think you will have better luck at Webmasters webmasters.stackexchange.com Commented Aug 28, 2022 at 13:32
  • Well, if you are not sure then take a look at one of the many, many existing answers to that question here on SO. That should get you started. Commented Aug 28, 2022 at 13:54

2 Answers 2

1

After breaking the server numerous times, I have finally fixed this myself. I'm not sure if this is the best answer, but just a reference for anyone who might need it:

RewriteCond %{REQUEST_URI} ^/([_0-9a-z-]+)$ [NC]
RewriteRule ^(.*)$ folder/to/file?q=$1 [L]
Sign up to request clarification or add additional context in comments.

4 Comments

You don't need the additional condition (RewriteCond directive)... put the regex in the RewriteRule pattern intsead. eg. Your 2 directives are the same as RewriteRule ^([\w-]+)$ folder/to/file?q=$1 [L]. The \w shorthand character class is the same as [_0-9a-zA-Z] so also negates the need for the NC flag. However, this is not a "redirect" as you've stated in the question, but an internal "rewrite". (A "redirect" generally implies an external HTTP redirect, which would be odd to implement here.)
However, this does not rewrite URLs of the form /something/page (your first example)?
oh yes you are right! I will have to add one more rule for that: RewriteCond %{REQUEST_URI} ^/([_0-9a-z-]+)/([_0-9a-z-]+)?$ [NC] RewriteRule ^(.*)$ folder/to/file?q=%1/%2 [L] which I bet there must be a better version.
You shouldn't need to add another rule to handle /something/page. The existing rule can be easily adapted to handle both (see my answer). And as mentioned, the preceding RewriteCond directive is not required here.
0
RewriteCond %{REQUEST_URI} ^/([_0-9a-z-]+)$ [NC]
RewriteRule ^(.*)$ folder/to/file?q=$1 [L]

Following on from your answer and my earlier comments... there's no need for the preceding RewriteCond directive as the necessary regex can be performed in the RewriteRule directive itself, which is also more efficient. For example, the above is equivalent to the following:

RewriteRule ^([\w-]+)$ folder/to/file?q=$1 [L]

The \w shorthand character class is the same as [_0-9a-zA-Z] which also negates the need for the NC (nocase) flag.

However, this rule does not handle URLs of the form /something/page (with two path segments) as in your first example. You don't need a separate rule for this if you simply want to copy the URL-path in it's entirely to the query string (as in your example).

For example, the following would suffice:

RewriteRule ^[\w-]+(/[\w-]+)?$ folder/to/file?q=$0 [L]

The above matches both /another and /something/page.

Note that the $0 backreference (as opposed to $1) contains the entire URL-path that is matched by the RewriteRule pattern.

3 Comments

Thanks for this! But I tried both of them and it didn't work, got a "not found" error.
@phantomhive Hhhmm, but the first rule is exactly equivalent to the rule in your question. So, if your rule worked then that rule must also "work" (for single path segment URLs)?? Presumably file is a physical file with extension, like file.php?
yes it is file.php. I'm not sure why too, but I tried the earlier RewriteRule ^([\w-]+)$ folder/to/file?q=$1 [L] too and it simply shows a 404.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.