Last active
May 6, 2021 12:45
-
-
Save smhmic/6296417 to your computer and use it in GitHub Desktop.
basic htaccess rules for pretty URLs and avoiding duplicate content (for a filesystem-based URL structure)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<IfModule mod_rewrite.c> | |
RewriteEngine On | |
# Redirect secondary domains | |
RewriteRule .* - [E=PRIMARY_DOMAIN:________________] | |
RewriteCond %{HTTP_HOST}==%{ENV:PRIMARY_DOMAIN} !^(.+)==\1$ [NC] | |
RewriteCond %{REQUEST_URI} ^/?(.*)$ | |
RewriteRule .? http://%{ENV:PRIMARY_DOMAIN}/%1 [L,QSA] | |
# Strip trailing slash | |
# Enforce no trailing slash | |
DirectorySlash Off | |
RewriteRule ^(.*)/$ /$1 [L,R] | |
# Allow no trailing slash (avoid 403 error) | |
RewriteCond %{REQUEST_FILENAME} -d | |
RewriteCond %{REQUEST_FILENAME}/index.php -f | |
RewriteRule .? %{REQUEST_FILENAME}/index.php [L,QSA] | |
RewriteCond %{REQUEST_FILENAME} -d | |
RewriteCond %{REQUEST_FILENAME}/index.html -f | |
RewriteRule .? %{REQUEST_FILENAME}/index.html [L,QSA] | |
# Hide extensions | |
# enforce no trailing index/index.php/index.html in URL | |
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?]*/)?index(\.php|\.html)?(\?.*)?\ HTTP/ | |
RewriteRule .? /%1 [R,L,QSA] | |
# enforce no .php/.html extension in URL | |
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?]*)\.(php|html)(\?.*)?\ HTTP/ | |
RewriteRule .? /%1 [R,L,QSA] | |
# allow no .php/.html extension | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteCond %{REQUEST_FILENAME}.php -f | |
RewriteRule .? %{REQUEST_FILENAME}.php [L,QSA] | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteCond %{REQUEST_FILENAME}.html -f | |
RewriteRule .? %{REQUEST_FILENAME}.html [L,QSA] | |
# No server 404 | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule .? / [R=302,L] | |
#ErrorDocument 404 / | |
</IfModule> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment