7
\$\begingroup\$

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]
\$\endgroup\$
7
  • 1
    \$\begingroup\$ Don't know whether this helps anyone, but this is something that helped me rewrite my .htaccess file. \$\endgroup\$ Commented Feb 21, 2015 at 0:31
  • \$\begingroup\$ Thanks @PopeyGilbert, this is a pretty great documentation that we will definitely use, but not really appropriated to the actual needs. Still, cool link ! \$\endgroup\$
    – G.EGCB
    Commented Feb 21, 2015 at 2:54
  • \$\begingroup\$ It's quite common to just redirect all the requests to a bootstrapping script, which will then handle the rest of the routing logic. You get the advantage of having a single point of entry for your application. \$\endgroup\$
    – Sam Dufel
    Commented Feb 24, 2015 at 20:49
  • \$\begingroup\$ Thanks for your comment @SamDufel! As I'm not that good in english and just for me to understand well your answer; A bootstraping script is kinda like what we did here.. right? Sorry for the confusion :) \$\endgroup\$
    – G.EGCB
    Commented Feb 24, 2015 at 21:29
  • 1
    \$\begingroup\$ Doing the regexes in PHP will probably be slower or more resource-intensive than going them in .htaccess. I would not suggest doing that unless you have additional processing that .htaccess can't do, or you're doing something else (i.e. not using regexes). \$\endgroup\$
    – Josh
    Commented Jun 17, 2015 at 0:56

1 Answer 1

3
\$\begingroup\$

Unfortunately I don't think there is much you can do here because of how the targets of your regexes are different. I was going to say you could change some of these to use groups, for example:

RewriteRule blog/(.*)/([0-9]+)-(.*)$ blogpost.php?brand=$1&blog_id=$2&titre=$3
RewriteRule brand/([0-9]+)-(.*) brand.php?id=$1&brand=$2
RewriteRule shop/brands/([0-9]+)-(.*) shopbrand.php?id=$1&brand=$2

These all end with the pattern ([0-9]+)-(.*) so you almost could extract the first part and use that file, like this:

RewriteRule (.*)/([0-9]+)-(.*)$ $1?id=$2&brand=$3

However you can't because e.g. shop/brands doesn't match shopbrand.php (and not everything has brand=). There just isn't enough commonality in the paths to make this sort of thing feasible.

If your goal is to simply get rid of .htaccess, using a script and using a dispatcher with PHP logic would be an alternative, as stated in the question comments. Personally I don't think that would be any simpler though, I think it would just be moving the "problem" into a different file--one that Apache needs to start up a PHP thread/process to solve, instead of doing it on its own.

The only way, IMHO, to improve this situation would be to architect the site differently and/or abandon the old URLs so you don't need to rewrite them. If you can change what the URLs are, then I would suggest switching to a single index.php dispatcher and MVC or REST pattern (or use a framework that handles this, like Symfony).

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.