4

I've looked around but frankly I am not sure of the lingo I need to be searching with to get more exacting results. Heck, I am struggling with url vs. uri vs. page vs. link and what each is and isn't. And the MOD_REWRITE guides seem to be written for people well-versed in Apache or Unix or whatever that is, especially when none of them seem to explain or parse down the examples to what each "programming" character does or means. Here and across Google I don't seem to get anything I want in the top 25 results of the searches I've tried covering the concept of...

Well, what I am trying to do is replace wildcard strings of an URL dependent upon three variables while preserving other bits. Is this possible using mod_rewrite?

For example

Three requested URLs are:

  1. http://mydomain.com/WORDS-AND-DASHES-c-NUMBERS_WITH_UNDERSCORES
  2. http://mydomain.com/WORDS-AND-DASHES-m-NUMBERS_WITH_UNDERSCORES
  3. http://mydomain.com/WORDS-AND-DASHES-p-NUMBERS_WITH_UNDERSCORES

The rewritten URLs, respectively, should be:

  1. http://mydomain.com/index.php?index&categories_id=NUMBERS_WITH_UNDERSCORES
  2. http://mydomain.com/index.php?main_page=index&mfrs_id=NUMBERS_WITH_UNDERSCORES
  3. http://mydomain.com/index.php?main_page=index&products_id=NUMBERS_WITH_UNDERSCORES

The WORDS-AND-DASHES will vary in content and length from page to page as will the NUMBERS_WITH_UNDERSCORES. The only constants are the domain and the three link type identifiers, -c-, -n- and -p-. The WORDS-AND-DASHES get replaced with a variable depending upon link type, which is eliminated, and the NUMBERS_WITH_UNDERSCORES remain in the new URL.

IOW if an url contains "-c-" then I want to replace everything between the domain and "-c-" with "index.php?index&categories_id=", remove the "-c-" portion and preserve the NUMBERS_WITH_UNDERSCORES. It's possible only a number follows -c-, -m-, or -p-, too.

If I knew PHP this probably possible, but I don't, and I am hoping mod_write is as powerful as I think it ought to be. If it takes multiple rules per variable, I'm fine with that too. Doing three times the work is still better than 10000+ times.

FWIW, during an upgrade it was discovered that the old URL rewriting module stopped functioning, so I am trying to quickly rebuild .htaccess and sitemap.xml for the search engines and all of the dead links in their respective results.

...then again I might be barking up the Mangled Metaphorbial Creek.

Thanks for whatever knowledge, directions to head, and/or advice you share.

-BC

2 Answers2

1

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

RewriteEngine On
RewriteRule ^([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.

Salman A
  • 262,204
  • 82
  • 430
  • 521
0

This may be overly simplified, but try:

RewriteEngine On
RewriteRule ^(.*?)-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]
DaveRandom
  • 87,921
  • 11
  • 154
  • 174