Convert NGINX rewrite rules to Apache htaccess

Viewed 232

I have a client who is moving away from an NGINX webserver to Apache. Everything is simple nothing complicated however since I'm an NGINX kinda guy I forgot how to convert the NGINX rewrite rules onto apache ones.

For example, these are the NGINX rewrites

rewrite ^/Tower-Topics-Calendar/?$ https://$host/events/ permanent;

How would I convert something like that onto .htaccess to use with Apache?

1 Answers
rewrite ^/Tower-Topics-Calendar/?$ https://$host/events/ permanent;

This looks like an external 301 redirect from /Tower-Topics-Calendar (with and without a trailing slash) to https://<host>/events/ - where <host> is the same hostname from the request and you specifically state the HTTPS protocol in the target.

In .htaccess you can achieve this using mod_rewrite. For example:

RewriteEngine On
RewriteRule ^Tower-Topics-Calendar/?$ https://%{HTTP_HOST}/events/ [R=301,L]

Note the absence of the slash prefix in the RewriteRule pattern.

However, if you don't specifically need to include the HTTPS scheme (ie. this is already canonicalised) then you can use a single mod_alias RedirectMatch directive instead. For example:

RedirectMatch 301 ^Tower-Topics-Calendar/?$ /events/

OR, to include the HTTPS protocol, you need to hardcode the hostname:

RedirectMatch 301 ^Tower-Topics-Calendar/?$ https://example.com/events/
Related