.htaccess REQUEST URI parsing

Hi, how are you?

I am trying to implement a “commands” system that allows someone in our organization to create commands that customize our homepage.

Basically I want to analize, say:


to extract whats between “…com/” and the first “/” after that. In the example above it would be “cmdthiscommand”.

Once I extract that, I want to verify if the first three letters are “cmd”. In which case I would proceed to decompose that word to extract “thiscommand”.

Then finish the request by building and passing the command in this way:


I want to do all of this using mod_rewrite in an .htaccess file.

Now, it is important to note that if the word that was extracted (in this case “cmdthiscommand”) does not start with the three characters “cmd”, I want the rewrite process to die and let the normal request procedures continue as they normally do. Meaning that if someone were to request http://www.edgeracing.com/support/ the url would not get re-written and they would actually get what the url is supposed to.

As of right now my .htaccess file contains this:

Options -Indexes
php_value include_path :/home/www/include:/home/www/db:/home/www/stats:/home/www/stats/support
ErrorDocument 400 /module/response/?cmd=badrequest
ErrorDocument 401 /module/response/?cmd=authorizationrequired
ErrorDocument 403 /module/response/?cmd=forbidden
ErrorDocument 404 /module/response/?cmd=notfound
ErrorDocument 408 /module/response/?cmd=requesttimedout
ErrorDocument 500 /module/response/?cmd=internalservererror
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/module/response/$ [NC]
RewriteCond %{QUERY_STRING} ^cmd=notfound$ [NC]
RewriteCond %{THE_REQUEST} ^GET[[:space:]](.+)[[:space:]]HTTP/ [NC]
RewriteRule ^.*$ - [E=ERROR404:%1]
RewriteCond %{ENV:ERROR404} \\.(jpe?g|gif|png)$ [NC]
RewriteRule ^.*$ /media/image/spacer.gif [L]

But I have to confess that the rules applied here that actually replace non-existent or erroneous image requests for spacer.gif where not created by me. The credit should go to pippo (Sitepoint Advisor of the Year).

Thanks for the help,

to achieve your original goal I’d use these rules:

(.htaccess in your document root)

RewriteEngine On
# If the URI is a command
# extract the command name from the URI
RewriteCond %{REQUEST_URI} ^/cmd([^/.]+)/?$ [NC]
# and then call index.php passing to it the commandname
RewriteRule . /index.php?cmd=%1 [L]

will call

The command name should not contain a dot.

let me know,