0

I have an htaccess file that starts out like:

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING}  where-can-i-find-information-about-homer-training
RewriteRule ^(.*)$  http://usersupport.homerenergy.com/customer/en/portal/articles/2186858-where-can-i-find-information-about-homer-training-?  [R=301,L]

RewriteCond %{QUERY_STRING}  is-homer-pro-conservative-in-its-predictions
RewriteRule ^(.*)$  http://usersupport.homerenergy.com/customer/en/portal/articles/2186859-is-homer-pro-conservative-in-its-predictions-?  [R=301,L]
...

which goes on for 308 more similar rules. As you can see, this is meant to redirect a list of articles from one site to another, where the matching element is the article name in the query part of the GET string.

If I just put in one or two of the pairs of RewriteCond/RewriteRules in the file, this works. But when I use the entire file, I get a 500 Internal Server Error.

Evidently, one of the rules is causing the error, but scanning the file I cannot see which it is.

Looking at the error log, I can see a message like this:

/var/www/html/.htaccess: RewriteCond: bad argument line '%{QUERY_STRING}'

which does not tell me much.

How can I tell which rule is causing this error?

MrWhite
  • 12,647
  • 4
  • 29
  • 41
rixter
  • 103
  • 3
  • My guess is that you are missing the _condPattern_ (2nd argument) on the problematic `RewriteCond` directive. Which version of Apache are you running? Depending on the version, you can (temporarily) enable more detailed (error) logging which will help pinpoint exactly where the error is occuring. – MrWhite Oct 29 '16 at 00:36
  • You can use 'LogLevel debug rewrite:trace8' for more log informations – Froggiz Oct 29 '16 at 11:00

1 Answers1

1

I believe you need to put your test values like so:

https://httpd.apache.org/docs/trunk/rewrite/remapping.html (section: Rewrite query string)

RewriteCond "%{QUERY_STRING}"  "where-can-i-find-information-about-homer-training"
RewriteRule "^(.*)$"  "http://usersupport.homerenergy.com/customer/en/portal/articles/2186858-where-can-i-find-information-about-homer-training-?" [R=301,L]

You may have a space in one of your values I'd guess. Give that a try to start, might fix it. I don't generally use " for rewrites, but I also don't generally use spaces, well I mean, I never use spaces in these types of names.

Just as an aside,

^(.*)$ 

doesn't really have any point, because

(.*)

already means: all of it. So starting and ending all is a bit redundant I believe.

A possible alternate error is that you simply missed a line break in the series of 500, might double check that using line number view in your code editor.

Lizardx
  • 210
  • 1
  • 9
  • Thanks, @Lizardx, that put me on the right track. Using this excellent resource, http://www.htaccesscheck.com/index.html, I was able to debug the rest of the errors. Found that having quotes around strings in the RewriteRule cause a 'bad flag delimiters' error. – rixter Oct 31 '16 at 17:20
  • I was wondering about those " around strings too, I'd never used them, was actually surprised to see them in the apache rewrite docs. Can you post an example of the exact string that flipped the error? I'm curious. An example that shows the line that trips error vs the corrected line. – Lizardx Oct 31 '16 at 19:17
  • I was curious about your htaccess check resource, and put a big very complicated .htaccess file in it to make sure it's not giving false error messages, and it all checked, so that looks like a good resource that may actually work. apache htaccess/config syntax has always been very difficult to master, I spent years originally getting to understand how to fix those 500 error messages, which as debugging help go, aren't very helpful, lol. – Lizardx Oct 31 '16 at 19:26