Skip to content

Instantly share code, notes, and snippets.

@ozkanozcan
Created April 3, 2022 10:52
Show Gist options
  • Save ozkanozcan/7b6cbaf1716d9452b7a067494945e388 to your computer and use it in GitHub Desktop.
Save ozkanozcan/7b6cbaf1716d9452b7a067494945e388 to your computer and use it in GitHub Desktop.
htaccess 301 redirect samples
# Non-www to www URLs 301 Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ozkanozcan.com$
RewriteRule (.*) http://www.ozkanozcan.com/$1 [R=301,L]
</IfModule>
# www to non-www URLs 301 Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.ozkanozcan.com$
RewriteRule (.*) http://ozkanozcan.com/$1 [R=301,L]
</IfModule>
# Http to Https URLs 301 Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# Https to Http URLs 301 Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# Domain to Another Domain 301 Redirect
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ http://ozkanozcan.com/$1 [R=301,L]
</IfModule>
# Pages With to Without Extension(html and htm) 301 Redirect
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?(.*).(html|htm)$ /$1 [R=301,L]
</IfModule>
#Subdomain to Sudirectory 301 Redirect blog.ozkanozcan.com to ozkanozcan.com/blog
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ http://ozkanozcan.com/blog/$1 [L,NE,R=301]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment