title: Optional URL variables
body: <p>Is there a way to define URLs with optional URL params in Flask? Essentially, what I'd like to do is define rules that allow for optionally specified languages:</p>
  
  <pre><code>/
  /de -&gt; matches / (but doesn't collide with /profile)
  /profile 
  /de/profile
  </code></pre>
  
  <p>I think I've figured out a way to do it, but it involves either making a change to how Werkzeug and Flask handles the request (either monkey patching or forking the framework source). This seems like an overly complex way to deal with this problem though.. Is there an easier way to do this that I'm overlooking?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Based on Brian's answer, here's what I came up with:</p>
  
  <p><strong>app.py</strong>:</p>
  
  <pre><code>from loc import l10n
  
  def create_app(config):                                                                                                                 
      app = Flask(__name__)                                                                                                               
      app.config.from_pyfile(config)                                                                                                      
  
      bp = l10n.Blueprint()                                                                                                               
      bp.add_url_rule('/', 'home', lambda lang_code: lang_code)                                                                           
      bp.add_url_rule('/profile', 'profile', lambda lang_code: 'profile: %s' % 
          lang_code)
      bp.register_app(app)                                                                                                                
  
      return app  
  
  if __name__ == '__main__':
      create_app('dev.cfg').run()
  </code></pre>
  
  <p><strong>loc/l10ln.py</strong></p>
  
  <pre><code>class Blueprint(Blueprint_):
      def __init__(self):
          Blueprint_.__init__(self, 'loc', __name__)
  
      def register_app(self, app):
          app.register_blueprint(self, url_defaults={'lang_code': 'en'})                                                                  
          app.register_blueprint(self, url_prefix='/&lt;lang_code&gt;')
  
          self.app = app
  </code></pre>
  
  <p>(I haven't gotten to pulling <code>lang_code</code> from the variable list yet, but will be doing that shortly)</p>
  
  <p>Now that's just hot imho.</p>