I’m starting on a website project where I want to rewrite urls with a dynamic length. For example, the following url’s can exist in the site:
http://www.url.com/signup/
Or
http://www.url.com/profile/mark/pm/compose/
Now I’ve got a .htaccess script working, but I doubt it’s efficiency. Here’s the code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&subone=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?page=$1&subone=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php? page=$1&subone=$2&subtwo=$3
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?page=$1&subone=$2&subtwo=$3
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&subone=$2&subtwo=$3&subthree=$4
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?page=$1&subone=$2&subtwo=$3&subthree=$4
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?arg[0]=$1&arg[1]=$2&arg[2]=$3&arg[3]=$4&arg[4]=$5
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?arg[0]=$1&arg[1]=$2&arg[2]=$3&arg[3]=$4&arg[4]=$5
I actually want to do something like this, but I can’t figure out how I get it working…
RewriteRule ^{[a-zA-Z0-9_-]+}/$ index.php?urlvar={$#}
So the url
http://www.url.com/profile/mark/pm/compose/
Will result in
http://www.url.com/index.php?urlvar[0]=profile&urlvar[1]=mark&urlvar[2]=pm&urlvar[3]=compose
Anyone whow knows how to fix this?
I got this working:
In .htaccess:
In index.php: