Skip to content

Instantly share code, notes, and snippets.

@strlns
Last active March 20, 2023 00:20
Show Gist options
  • Save strlns/a43c676b32a680a4d4a07714234a2032 to your computer and use it in GitHub Desktop.
Save strlns/a43c676b32a680a4d4a07714234a2032 to your computer and use it in GitHub Desktop.
Apache RewriteEngine simple reminder (Apache 2.4+)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# OR has a HIGHER precedence than the implicit AND of subsequent RewriteCond directives.
RewriteCond %{REQUEST_URI} !/app/assets/.*
# Disallow everything below /app/
RewriteCond %{REQUEST_URI} /app/.* [OR]
# Disallow dotfiles
RewriteCond %{REQUEST_URI} /\..*
RewriteRule ^.* - [F,L]
# Redirect a direct requests to folders to index.php
RewriteCond %{REQUEST_URI} /.*/
RewriteCond %{REQUEST_URI} !/.*/[^/]+$
RewriteRule ^(.*)$ ?dir=$1 [L,R=307]
</IfModule>

It's been a while since I last had to configure redirects in Apache .htaccess, and the complexity and documentation is overwhelming if one does not configure Apache regularly.

So here's 3 simple examples demonstrating some features:

  • RewriteRule with included condition, no RewriteCond
  • RewriteRule with multiple RewriteCond directives
  • RewriteRule with regex backreference
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment