Can mod_rewrite find a variable within an URL, replace a portion before it, delete the variable and preserve what remains? Can mod_rewrite find a variable within an URL, replace a portion before it, delete the variable and preserve what remains? apache apache

Can mod_rewrite find a variable within an URL, replace a portion before it, delete the variable and preserve what remains?


The following rules should work. They must be placed inside the .htaccess file in the root directory of your website.

RewriteEngine OnRewriteRule ^([a-z-]+)-c-([0-9_]+) index.php?index&categories_id=$2         [NC]RewriteRule ^([a-z-]+)-m-([0-9_]+) index.php?main_page=index&mfrs_id=$2     [NC]RewriteRule ^([a-z-]+)-p-([0-9_]+) index.php?main_page=index&products_id=$2 [NC]

Apache will fire index.php when the URLs you mentioned in your question are requested. Apache will populate the query string accordingly. If WORDS-AND-DASHES contains something other than a-z and - then modify the rules accordingly.


This may be overly simplified, but try:

RewriteEngine OnRewriteRule ^(.*?)-c-([\d_]+)$ index.php?index&categories_id=$2         [L]RewriteRule ^(.*?)-m-([\d_]+)$ index.php?main_page=index&mfrs_id=$2     [L]RewriteRule ^(.*?)-p-([\d_]+)$ index.php?main_page=index&products_id=$2 [L]