Skip to content

Instantly share code, notes, and snippets.

@arturmamedov
Last active January 7, 2023 01:36
Show Gist options
  • Save arturmamedov/3ebfcc784ed16aa066d2c6a98dcab14d to your computer and use it in GitHub Desktop.
Save arturmamedov/3ebfcc784ed16aa066d2c6a98dcab14d to your computer and use it in GitHub Desktop.
Rewrite Rules - must used
# 0 - Before any rule check for mod-rewrite module existance and enable it
<IfModule mod_rewrite.c>
# Enable before other rules
RewriteEngine On
</IfModule>
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Normal Rewrite of a url to another
RewriteRule ^le-case-mobili$ /alloggio/le-case-mobili [R=301,L]
# 1 - Folder rewrite
RewriteRule ^theme_fe_gs/(.*)$ http://www.obiv.it/new/newest/theme_fe_gs/$1 [L]
# 2 - 301 Rediret with structural change in url
RewriteRule ^corporate/(.*)$ http://www.gestionecampeggi.it/$1 [R=301,L]
# 3 - Redirect for params
RewriteCond %{QUERY_STRING} lang=eng [NC]
RewriteRule ^home.php /en/home? [R=301,L]
# 4 - Redirect all calls to `webroot` folder (cakephp, in laravel `public`)
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
# 5 - Handle Authorization Header (set the enviorenment constant)
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# 6 - Set %{ENV:proto} variable, to allow rewrites to redirect with the
# appropriate schema automatically (http or https by using %{ENV:proto} constant).
# Not need if you force https
RewriteCond %{HTTPS} =on
RewriteRule ^ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^ - [env=proto:http]
# 7 - Redirect from the `http://` to the `https://` version of the URL.
# https://wiki.apache.org/httpd/RewriteHTTPToHTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# 8 - Forcing the `www.` at the beginning of URLs
# Rewrite example.com → www.example.com
# Be aware that the following might not be a good idea if you use "real"
# subdomains for certain parts of your website.
# RewriteCond %{HTTPS} !=on # no if you force https, and your certificate support ssl with www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_ADDR} !=127.0.0.1
RewriteCond %{SERVER_ADDR} !=::1
RewriteRule ^ %{ENV:proto}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# 9 - Suppressing the `www.` at the beginning of URLs
# rewrite www.example.com → example.com
# RewriteCond %{HTTPS} !=on # no if you force https, and your certificate support ssl without www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]
@arturmamedov
Copy link
Author

arturmamedov commented Jan 10, 2022

Myne Personal Rewrite Rules that i use every where (see also: https://github.com/arturmamedov/withFront/blob/master/default.htaccess)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment