Use strict_slashes in Python Flask

Johan Louwers
4 min readMay 24, 2024

--

Flask is a popular micro web framework for Python, designed to help developers build web applications quickly and efficiently. Its lightweight nature provides a flexible and scalable foundation, making it ideal for both simple projects and complex, enterprise-level applications.

  • Simplicity and Flexibility: Flask’s minimalistic design gives developers the freedom to structure their applications as they see fit, without imposing strict patterns or conventions.
  • Extensibility: With a rich ecosystem of extensions, Flask can be easily customized and extended to meet specific needs, including database integration, authentication, and form validation.
  • Rapid Development: Flask’s straightforward setup and intuitive API empower developers to move swiftly from concept to deployment, fostering an agile development process.
  • Scalability: Despite its simplicity, Flask is robust enough to handle high-traffic applications, thanks to its modular architecture and compatibility with various production-ready web servers.

Whether you are a startup looking to launch a new product or an established enterprise aiming to innovate, Flask provides a reliable and efficient solution for developing web applications that can grow with your business.

Using strict_slashes

In Python Flask, the strict_slashes option in route decorators controls whether a URL endpoint should strictly differentiate between URLs with and without a trailing slash. By default, Flask distinguishes between these two, treating them as separate routes. However, strict_slashes allows you to configure this behavior according to your application needs.

By default, Flask treats URLs with and without a trailing slash as distinct endpoints. For example, consider the following route definition:

@app.route('/example')
def example():
return "This is the example endpoint"

With this configuration:

  • Accessing /example will work and return the expected response.
  • Accessing /example/ will result in a 404 Not Found error because Flask considers it a different endpoint.

Using strict_slashes=False

When strict_slashes is set to False, Flask will not differentiate between URLs with and without a trailing slash. This means that both variations of the URL will be treated as the same endpoint. Here’s how you can configure it:

@app.route('/example', strict_slashes=False)
def example():
return "This is the example endpoint"

With this configuration:

  • Accessing /example will work and return the expected response.
  • Accessing /example/ will also work and return the same response.

Benefits of strict_slashes=False

  1. User-Friendly URLs: Users are not always consistent in typing URLs with or without trailing slashes. By setting strict_slashes=False, you ensure that both variations work seamlessly, improving the user experience.
  2. SEO Consistency: Search engines treat URLs with and without trailing slashes as different pages, which can lead to duplicate content issues. Ensuring that both variations point to the same content can help maintain consistency in SEO practices.
  3. Development Flexibility: Developers can define routes without worrying about trailing slashes, simplifying the development process and reducing potential errors related to URL handling.

Use Cases

  • APIs: In RESTful API design, it’s common practice to be lenient with trailing slashes to avoid confusion among API consumers.
  • Web Applications: For web applications with numerous routes, ensuring that both URL formats work can prevent user frustration and reduce the likelihood of encountering 404 errors due to minor URL differences.

Implementation Example

Here’s a comprehensive example showing how to use strict_slashes=False in various routes:

from flask import Flask

app = Flask(__name__)

@app.route('/', strict_slashes=False)
def root():
return "This is the root endpoint"

@app.route('/data', strict_slashes=False)
def data():
return "This is the data endpoint"

@app.route('/img', strict_slashes=False)
def img():
return "This is the img endpoint"

@app.route('/img/graph', strict_slashes=False)
def img_graph():
return "This is the img/graph endpoint"

@app.route('/img/graph/line', strict_slashes=False)
def img_graph_line():
return "This is the img/graph/line endpoint"

@app.route('/img/graph/line/example', strict_slashes=False)
def img_graph_line_example():
return "This is the img/graph/line/example endpoint"

if __name__ == '__main__':
app.run(debug=True)

In this example, all routes are configured to accept both formats, providing a consistent and user-friendly interface.

Conclusion

The strict_slashes parameter in Flask route decorators is a powerful tool for controlling URL endpoint behavior. Setting strict_slashes=False ensures that your application treats URLs with and without trailing slashes as identical, enhancing usability, SEO consistency, and development flexibility. This approach is especially beneficial for applications with diverse user bases and extensive routing requirements.

About the author(s)
Johan Louwers is currently Chief Enterprise Architect within Oracle as well as the lead architect for NATO and a number of militaries. Johan has a strong and long background in the field of Enterprise Architecture and complex system engineering. Having worked with enterprises in a diverse set of industries as (enterprise) architect, CTO and technical and strategic business advisor Johan brings both deep technical knowledge to the table as well as strong business oriented expertise. In addition to this Johan is a tech addict who tends to enjoy supporting open source initiatives and actively coding as a hobby. Views expressed in this post are personnel and do not necessarily reflect the views of my current employer.

--

--

Johan Louwers

Johan Louwers is a technology enthousiasts with a long background in supporting enterprises and startups alike as CTO, Chief Enterprise Architect and developer.