patternMinor
An .htaccess for our entire site
Viewed 0 times
entirehtaccesssiteforour
Problem
We have this long
```
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&us
.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&us
Solution
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:
These all end with the pattern
However you can't because e.g.
If your goal is to simply get rid of
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
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=$2These 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=$3However 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).Code Snippets
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=$2RewriteRule (.*)/([0-9]+)-(.*)$ $1?id=$2&brand=$3Context
StackExchange Code Review Q#82127, answer score: 3
Revisions (0)
No revisions yet.