We have this long .htaccess
mainly rewriting our URLs. Everything works fine (except blog titles containing a '
, but we're working on that) but I'm sure it can be shorter. Feel free to review our code and propose some generic or simpler ways to rewrite the URLs instead of rewriting each and every concerned page like the example below.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^oursite\.site$ [OR]
RewriteCond %{HTTP_HOST} ^www\.oursite\.site$
RewriteRule ^admin\/?$ "http\:\/\/oursite\.site\/admin\/connexion" [R=301,L]
# Rewrite spaces as long as there are more than 1 spaces in URI
RewriteRule "^(\S*)\s+(\S*\s.*)$" /$1-$2 [L,NE]
# Rewrite spaces when there is exactly 1 space in URI
RewriteRule "^(\S*)\s(\S*)$" /$1-$2 [L,R=302,NE]
#Redirect non-www to www
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#Rewrite urls blog
RewriteRule blog/(.*)/([0-9]+)-(.*)$ blogpost.php?brand=$1&blog_id=$2&titre=$3
#Rewrite urls brands
RewriteRule brand/([0-9]+)-(.*) brand.php?id=$1&brand=$2
#Rewrite urls shop brands
RewriteRule shop/brands/([0-9]+)-(.*) shopbrand.php?id=$1&brand=$2
#Rewrite urls shop category
RewriteRule shop/category/([0-9]+)-(.*) shopcategory.php?id=$1&cat_nom=$2
#Rewrite ADMIN profile
RewriteRule admin/([0-9]+)-([a-zA-Z0-9\-]+)/profil$ admin/profil.php?user_id=$1&brand=$2
#Rewrite ADMIN panel
RewriteRule admin/([0-9]+)-([a-zA-Z0-9\-]+)/panel$ admin/panel.php?id=$1&brand=$2
#Rewrite ADMIN panel-blog
RewriteRule admin/([0-9]+)-([a-zA-Z0-9\-]+)/blog$ admin/panel-blog.php?id=$1&brand=$2
#Rewrite ADMIN panel-blog-add
RewriteRule admin/([0-9]+)-([a-zA-Z0-9\-]+)/blog-add$ admin/panel-blog-add.php?user_id=$1&user_brand=$2
#Rewrite ADMIN panel-blog-modif
RewriteRule admin/([0-9]+)-([a-zA-Z0-9\-]+)/blog-modif$ admin/panel-blog-modif.php?id=$1&brand=$2
#Rewrite ADMIN post-modif
RewriteRule admin/([0-9]+)-([a-zA-Z0-9\-]+)/post-modif/([0-9]+)$ admin/post-modif.php?user_id=$1&user_brand=$2&blog_id=$3
#Rewrite ADMIN add
RewriteRule admin/([0-9]+)-([a-zA-Z0-9\-]+)/add$ admin/add.php?user_id=$1&user_brand=$2
#Rewrite ADMIN update
RewriteRule admin/([0-9]+)-([a-zA-Z0-9\-]+)/update/([0-9]+)$ admin/update.php?user_id=$1&user_brand=$2&blog_id=$3
#Rewrite ADMIN delete
RewriteRule admin/([0-9]+)-([a-zA-Z0-9\-]+)/delete/([0-9]+)$ admin/delete.php?id=$1&brand=$2&blog_id=$3
#Rewrite ADMIN panel-prod
RewriteRule admin/([0-9]+)-([a-zA-Z0-9\-]+)/boutique$ admin/panel-prod.php?id=$1&brand=$2
#Rewrite ADMIN panel-prod-add
RewriteRule admin/([0-9]+)-([a-zA-Z0-9\-]+)/boutique-add$ admin/panel-prod-add.php?id=$1&brand=$2
#Rewrite ADMIN panel-prod-modif
RewriteRule admin/([0-9]+)-([a-zA-Z0-9\-]+)/boutique-modif$ admin/panel-prod-modif.php?id=$1&brand=$2
#Rewrite ADMIN prod-modif
RewriteRule admin/([0-9]+)-([a-zA-Z0-9\-]+)/produit-modif/([0-9]+)$ admin/prod-modif.php?id=$1&brand=$2&prod_id=$3
#Rewrite ADMIN prod-add
RewriteRule admin/([0-9]+)-([a-zA-Z0-9\-]+)/produit-add$ admin/prod-add.php?user_id=$1&user_brand=$2
#Rewrite ADMIN prod-update
RewriteRule admin/([0-9]+)-([a-zA-Z0-9\-]+)/produit-update/([0-9]+)$ admin/prod-update.php?id=$1&brand=$2&prod_id=$3
#Rewrite ADMIN prod-delete
RewriteRule admin/([0-9]+)-([a-zA-Z0-9\-]+)/produit-delete/([0-9]+)$ admin/prod-delete.php?id=$1&brand=$2&prod_id=$3
#Rewrite ADMIN faq
RewriteRule admin/([0-9]+)-([a-zA-Z0-9\-]+)/faq$ admin/faq.php?id=$1&brand=$2
#Remove php extensions
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
ErrorDocument 404 http://oursite.site/404
#CACHESYSTEM
<FilesMatch "(?i)^.*\.(ico|flv|jpg|jpeg|png|gif|js|css)$">
ExpiresActive On
ExpiresDefault A2592000
</FilesMatch>
#MAINTENANCE SYSTEM (if required)
#RewriteCond %{REQUEST_URI} !/maintenance.php$
#RewriteCond %{REMOTE_ADDR} !^555\.555\.555\.555
#RewriteRule $ /maintenance [R=302,L]