skip to Main Content

When a user requests /geo/anchorage.json from my server, I’m trying to have it provide data from /geo/a/n/c/anchorage.json

I have this rule written in my .htaccess file, but it’s causing a 500 internal server error.

RewriteRule ^geo/((.)(.)(.).+).json /geo/$2/$3/$4/$1.json [QSA,L]

I’ve broken down the rule into parts, testing the first part with a php script to output the parameters, and that worked fine.

RewriteRule ^geo/((.)(.)(.).+).json /geo/test.php?2=$2&3=$3&4=$4&1=$1 [QSA,L]

It seems like it’s the last part that’s causing the error, but I can’t find what I’m doing wrong. I’ve verified that /geo/a/n/c/anchorage.json exists on the server. Is there anything special when you use variables as folders?

2

Answers


  1. The resulting URL /geo/a/n/c/anchorage.json also matches the input regex (^geo/((.)(.)(.).+).json), so you’ll get a rewrite loop (500 error). You can avoid the rewrite loop by being more specific in your regex. eg. Instead of matching any character (.) you could match anything that is not a slash ([^/]).

    In other words, try the following:

    RewriteRule ^geo/((.)([^/])([^/])[^/.]+).json$ /geo/$2/$3/$4/$1.json [QSA,L]
    

    I left the first capturing group as a . (dot) since that couldn’t be a slash anyway.

    Login or Signup to reply.
  2. You may use this rule to fix your issue:

    RewriteRule ^(geo)/((w)(w)(w).*.json)$ $1/$3/$4/$5/$2 [NC,L]
    

    There is no need to use QSA flag as you’re not modifying query string.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search