Skip to content

Instantly share code, notes, and snippets.

@devluis
Last active December 29, 2015 19:09
Show Gist options
  • Save devluis/7715148 to your computer and use it in GitHub Desktop.
Save devluis/7715148 to your computer and use it in GitHub Desktop.
Htaccess tips.
# Error Pages
# Here error page is redirecting to error.html.
errorDocument 400 http://www.youwebsite.com/404.html
errorDocument 401 http://www.youwebsite.com/error.html
errorDocument 403 http://www.youwebsite.com/error.html
errorDocument 500 http://www.youwebsite.com/error.html
# Disable directory Listing
# If you want to disable folder files listing, include following code.
Options All -Indexes
# RewriteEngine On it is turn on Rewrite Rules in Apache Server. if you want to turn off, just change the value to off.
RewriteEngine On
# Domain Redirection
# .htacces code for redirecting yourwebsite.com to www.yourwebsite.com
RewriteCond %{HTTP_HOST} ^youwebsite.com
RewriteRule (.*) http://www.youwebsite.info/$1 [R=301,L]
# Sub Domain Redirection
# Sub domain redirection mapping to folder. Here http://www.yourwebsite.com is connecting to website_folder folder.
RewriteCond %{HTTP_HOST} ^subdomain\.youwebsite\.com$
RewriteCond %{REQUEST_URI} !^/subdomain_folder/
RewriteRule (.*) /subdomain_folder/$1
# Old Domain Redirection
# htaccess code for redirecting old domain(abc.com) to new domain(xyz.com).
RewriteCond %{HTTP_HOST} ^oldwebsite.com
RewriteRule (.*) http://www.newwebsite.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.oldwebsite\.com
RewriteRule (.*) http://www.newwebsite.com/$1 [R=301,L]
# Profile URL
# Profile parameter allows [a-zA-Z0-9_-] these inputs
# http://mydomain.com/profile.php?username=devluis
# to
# http://mydomain.com/devluis
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1
# Messages URL
# http://mydomain.com/messages.php?message_username=devluis
# to
# http://mydomain.com/messages/devluis
RewriteRule ^messages/([a-zA-Z0-9_-]+)$ messages.php?message_username=$1
RewriteRule ^messages/([a-zA-Z0-9_-]+)/$ messages.php?message_username=$1
# Friends URL
# http://mydomain.com/friends.php?username=devluis
# to
# http://mydomain.com/friends/devluis
RewriteRule ^friends/([a-zA-Z0-9_-]+)$ friends.php?username=$1
RewriteRule ^friends/([a-zA-Z0-9_-]+)/$ friends.php?username=$1
# Friends URL with Two Parameters
# Here the first parameter allows [a-zA-Z0-9_-] and second parameter allows only number [0-9]
# http://mydomain.com/friends.php?username=devluis&page=2
# to
# http://mydomain.com/friends/devluis/2
RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)$ friends.php?username=$1&page=$2
RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)/$ friends.php?username=$1&page=$2
# Hiding File Extension
# http://www.mydomain.com/index.html
# to
# http://www.mydomain.com/index
RewriteRule ^([^/.]+)/?$ $1.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment