A powerful, lightweight Flask application for intelligent URL redirection π
Seamlessly redirect all incoming requests while preserving path structures with enterprise-grade reliability β¨
|
Redirect all incoming requests to any external URL with zero configuration hassle |
Maintains original URL structure - no broken links, no lost paths |
|
Built-in request logging with timestamps for monitoring and analytics |
Production-ready containerization with security best practices |
|
Gunicorn WSGI server configuration for high-performance deployment |
Non-root user execution and secure container practices |
# π₯ Clone the repository
git clone https://github.com/khaliduzzamantanoy/Pyredirect.git
cd Pyredirect
# π¦ Install dependencies
pip install -r requirements.txt
# βοΈ Configure your redirect URL (edit app.py)
# Replace 'https://your-redirect-url/' with your target
# π― Run the application
python app.pyπ Access your app at: http://localhost:5000
# π¨ Build the Docker image
docker build -t pyredirect .
# π Run the container
docker run -p 5002:5002 pyredirectπ Access your app at: http://localhost:5002
π Click to expand configuration details
Edit the catch_all function in app.py:
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
# π Replace with your target URL
return redirect(f'https://your-awesome-site.com/{path}', code=302)| Feature | Status | Description |
|---|---|---|
| π Request Logging | β Enabled | Logs all incoming requests with timestamps |
| π Disable Logging | βοΈ Optional | Comment out @app.before_request decorator |
| π Custom Logging | π οΈ Extensible | Add your own logging logic |
| Environment | Port | Description |
|---|---|---|
| π Development | 5000 |
Default Flask development server |
| π³ Docker | 5002 |
Container exposed port |
| π Production | 5002 |
Gunicorn WSGI server |
graph TD
A[π Incoming Request] --> B[π Flask Catch-All Route]
B --> C[π Log Request]
C --> D[π€οΈ Extract Path]
D --> E[π Generate Redirect URL]
E --> F[β©οΈ HTTP 302 Redirect]
style A fill:#e1f5fe
style F fill:#f3e5f5
| Original URL | Redirects To | Status |
|---|---|---|
yourapp.com/ |
example.com/ |
β |
yourapp.com/about |
example.com/about |
β |
yourapp.com/api/users/123 |
example.com/api/users/123 |
β |
yourapp.com/blog/post?id=5 |
example.com/blog/post?id=5 |
β |
ποΈ Pyredirect/
βββ π app.py # Main Flask application
βββ π wsgi.py # WSGI entry point
βββ π³ Dockerfile # Docker configuration
βββ π¦ requirements.txt # Python dependencies
βββ π README.md # You are here!
# π¦ Install production server
pip install gunicorn
# π Run in production mode
gunicorn --bind 0.0.0.0:5002 app:appπ οΈ Advanced Gunicorn Settings
# π High-performance configuration
gunicorn --bind 0.0.0.0:5002 \
--workers 4 \
--timeout 30 \
--keep-alive 2 \
--max-requests 1000 \
app:app| Security Feature | Status | Description |
|---|---|---|
| π€ Non-root User | β | Container runs as unprivileged user |
| π Minimal Base Image | β | Python slim image reduces attack surface |
| π« No Root Privileges | β | Application runs with limited permissions |
| π Request Monitoring | β | Built-in logging for security analysis |
| Use Case | Description | Benefits |
|---|---|---|
| π β‘οΈπ’ Domain Migration | Redirect from old to new domain | SEO preservation, seamless transition |
| π§ Maintenance Mode | Redirect during site maintenance | User experience continuity |
| βοΈ Load Balancing | Simple traffic distribution | High availability, performance |
| π URL Shortening | Base for shortening services | Scalable, customizable |
| π§ͺ A/B Testing | Traffic splitting for testing | Data-driven decisions |
π‘ Advanced Redirect Patterns
@app.route('/<path:path>')
def smart_redirect(path):
# π― Conditional redirects based on path
if path.startswith('api/'):
return redirect(f'https://api.example.com/{path[4:]}', code=302)
elif path.startswith('blog/'):
return redirect(f'https://blog.example.com/{path[5:]}', code=302)
else:
return redirect(f'https://main.example.com/{path}', code=302)π Advanced Logging Setup
import logging
from datetime import datetime
# π Setup enhanced logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@app.before_request
def enhanced_logging():
logger.info({
'timestamp': datetime.now().isoformat(),
'method': request.method,
'url': request.url,
'user_agent': request.user_agent.string,
'remote_addr': request.remote_addr
})This project is licensed under the MIT License π
Feel free to use, modify, and distribute this project β¨
| Resource | Link | Description |
|---|---|---|
| π Issues | GitHub Issues | Bug reports & feature requests |
| π¬ Discussions | GitHub Discussions | Community support & ideas |
| π§ Contact | Create Issue | Direct support |
β Star this repo if you find it useful!
Made with β€οΈ by Khaliduzzaman Tanoy
PyRedirect - Simplifying URL redirection, one request at a time π
https://your-redirect-url/ in your code with your actual target URL before deployment!