1

I am a little lost. I tried searching this site and the web at large, but I couldn't seem to find exactly what I need. So please forgive me if this has been answered before, I really tried to find it.

I have … inherited a .htaccess file with quite a lot of 301 redirects in the

Redirect 301 /shorthand /actual-link/actual-file.php

syntax (with a RewriteEngine On line somewhere high up in the file). I don't know exactly much about redirects, but that seems pretty straightforward to me. It just sits there and sometimes new shorthand links get added. There is some non-www to www, and http to https kind of stuff at the top, that's it.

Now the structure of the site changes, and two similar pages that process query parameters get consolidated into one. Basically there is

Old page: /path/subpath/index.php?some=query&parameter=or&other

New page: /other-path/file.php?added=parameter&some=query&parameter=or&other

I can't predict what parameters exactly will be part of the URL, I just have to take everything starting from the ? and append it to the new URL, that already has to include an added parameter, so everything after the ? follows ?added=parameter& .

I suppose that is not exactly hard, but alas, I lack the experience. And all I could find was something like "Take this specific defined query parameter you already know and set it as a path name" or vice versa, and I couldn't get that to work for my problem.

Is there a solution compatible with the syntax used elsewhere in the file? Or does that matter at all? Can I combine Redirect 301 … lines with RewriteCond … RewriteRule … commands? Does %{QUERY_STRING} help me somehow? If so, how can I figure out the correct syntax?

I would really appreciate if someone could point me in the right direction. Many thanks in advance!

  • The `Redirect` directive should automatically append the query string to the new target URL again, so I am not really sure what the problem is supposed to be here in the first place ...? – CBroe Feb 15 '22 at 09:52
  • Thanks for the quick reply. I forgot to mention that the new page needs to have an additional parameter that the old page didn't have. I have edited the question accordingly. – perdittmann Feb 15 '22 at 09:58
  • 1
    Then you should do this using a RewriteRule, not Redirect. You add your new parameter in the substitution URL, and the `QSA` flag to get it merged with the existing query string of the originally requested URL. – CBroe Feb 15 '22 at 10:01
  • Ah, I see! Thanks. So – if I may bother you some more – something like `RewriteRule ^/path/subpath/index.php(.*)$ /other-path/file.php?added=parameter&$1 [QSA]` should work? Or did I get the syntax mixed up? – perdittmann Feb 15 '22 at 10:10
  • RewriteRule matches only against the path component of the URL, so `(.*)` after `index.php` doesn't make sense, that will never match anything. And when configure in .htaccess, the path does never start with a `/` either, that has been stripped off at this point already. – CBroe Feb 15 '22 at 10:16
  • If you wanted to match/look for specific parameters/value in the query string of the original URL, then that would need to be done using a RewriteCond. But you only want to append the original query string here, so that needs only the QSA flag. `RewriteRule ^path/subpath/index\.php /other-path/file.php?added=parameter [R=302,QSA]` (R=302 is always good for testing, prevents your browser from caching faulty redirects. Make that into a `301`, once everything works as desired.) – CBroe Feb 15 '22 at 10:19
  • Yes, that worked. Thank you so much for your support. I learned a lot today. Is there a good reference on htaccess syntax you could recommend? So far, I only found countless variations of "the five same things everyone needs, just for copy & paste without any explanation whatsoever". – perdittmann Feb 15 '22 at 11:28
  • https://stackoverflow.com/q/20563772/1427878 contains a good assemblage of basic explanations. And there is always the official documentation as well. – CBroe Feb 15 '22 at 11:31
  • Ah, that's great. Thank you! – perdittmann Feb 15 '22 at 12:20

0 Answers0