0

I have a WordPress site with WooCommerce installed and Avada theme. I am trying to do a rewrite rule via functions.php file.

I want that what arrives on http://example.com/product/SKU01 has to be redirected on http://example.com/custom-file.php?prod_sku=SKU01

add_action('init', 'dcc_rewrite_tags'); function dcc_rewrite_tags() { add_rewrite_tag('%propref%', '([^&]+)'); } add_action('init', 'dcc_rewrite_rules'); function dcc_rewrite_rules() { add_rewrite_rule('^product/?$','custom-file.php?prod_sku=$matches[1]','top'); } 

Within a plugin (Rewrite Rule Inspector) I can see that all is working but, when I try to open the URL on the frontend, it is not working.

This is not working at all. My WooCommerce doesn't use any "product" string in its URLs, but it is not working at all. I also tried with .htaccess but I get the same behavior. Every time I call /product/sku01 I am redirected to 404 page.

2
  • Have You visited permalink options page (I'm not sure if it is named "permalink" novadays)? Visit here fires some reload redirections method, which could help You. Commented Apr 17, 2018 at 9:58
  • @bigwolk yes but not working at all. I also flushed the Rewrite rules Commented Apr 17, 2018 at 10:01

2 Answers 2

2

Here you need to flush out the rewrite rules; for that, you can do it by going to Settings -> Permalinks and clicking on save button. add_rewrite_rule rules do not write something to the .htaccess file. So that you should flush out rewrite rules.

Change from

add_rewrite_rule('^product/?$','custom-file.php?prod_sku=$matches[1]','top'); 

TO:

add_rewrite_rule('^product/([^/]*)/([^/]*)/?','custom-file.php?prod_sku=$matches[1]','top'); 

http://codex.wordpress.org/Rewrite_API/flush_rules

Sign up to request clarification or add additional context in comments.

4 Comments

I am near a solution. I can't redirect on a php file different from index.php so I did a page with custom template. I keep you up-to-date
no errors. If I set a custom php file as I did, I have been redirected on 404. It is like, I have added no rules.
Can you write url on which you are getting 404
Nothing, not working. But I found a solution that I am posting it. Actually I don't know if it is the cleanest solution but it works. I think that the problem is that product in url is a string catched by woocommerce
0

I found a solution. I don't know if this is a clean solution but it works proper. I did a page on backend with a custom template. I am not sure about this but I think that when woocommerce is installed, all the urls that contain "product" are redirected on index.php. So my rewrite rule say: if contain "product" go on my custom page. Here below my code I put on functions.php file.

add_action('init', 'dcc_rewrite_tags'); function dcc_rewrite_tags() { add_rewrite_tag('%propref%', '([^&]+)'); } add_action('init', 'dcc_rewrite_rules'); function dcc_rewrite_rules() { add_rewrite_rule('^product/(.+)/?$','index.php?page_id=14964&propref=$matches[1]','top'); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.