Can mod_rewrite find the variable in the url, replace the part in front of it, delete the variable and save what is left?

I looked around, but to be honest, I'm not sure what I should look for in order to get more demanding results. Hell, I'm fighting url versus uri versus page versus link, and what is and is not. And the MOD_REWRITE manuals seem to be written for people well versed in Apache or Unix, or something else, especially when none of them explain or analyze examples of what each “programming” symbol does or means. Here and through Google, I don't seem to get anything I want in the top 25 search results I tried, encompassing the concept ...

Well, what I'm trying to do is replace the URL wildcards that depend on three variables, while preserving the other bits. Is this possible with mod_rewrite?

for instance

Three requested URLs:

  • http://mydomain.com/WORDS-AND-DASHES-c-NUMBERS_WITH_UNDERSCORES
  • http://mydomain.com/WORDS-AND-DASHES-m-NUMBERS_WITH_UNDERSCORES
  • http://mydomain.com/WORDS-AND-DASHES-p-NUMBERS_WITH_UNDERSCORES

Rewritten URLs, respectively, should be:

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

WORD AND-DASHES will vary in content and length from page to page, like NUMBERS_WITH_UNDERSCORES. The only constants are the domain and three link type identifiers, -c-, -n- and -p-. I-DASHWORDS are replaced with a variable depending on the type of link that is excluded, and NUMBERS_WITH_UNDERSCORES remain in the new URL.

IOW, if the url contains "-c-", then I want to replace everything between the domain and "-c-" with "index.php? Index & category_id =", delete the "-c-" part and save NUMBERS_WITH_UNDERSCORES. Perhaps only a number follows like -c-, -m- or -p-.

If I knew PHP, maybe this is possible, but I won’t do it, and I hope that mod_write will be as powerful as I think it should be. If several variables are required for each variable, I'm fine too. Performing triple work is even better than 10,000+ times.

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

... again, I could bark Mangled Metaphorbial Creek.

Thank you for any knowledge, guidance and / or advice that you share.

-BC

+4
source share
2 answers

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

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

Apache starts index.php when it asks for the URLs that you specified in your question. Thus, Apache will populate the query string. If WORDS-AND-DASHES contains anything other than az and - , change the appropriate rules.

+1
source

This may be oversimplified, 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] 
0
source

Source: https://habr.com/ru/post/1413485/


All Articles