skip to Main Content

I have a website with a products.html file. Now, inside this file, I will have some Javascript code that checks the url to show the correct products/categories. This are some examples of the urls:

example.com/products
example.com/products/
example.com/products/shoes
example.com/products/shoes/
example.com/products/shoes/adidas
example.com/products/shoes/adidas/
example.com/products/shoes/adidas/adidas-predator-20.3
example.com/products/shoes/adidas/adidas-predator-20.3/

So basically, I want that everytime someone goes to the any of the above urls, I want to get the file products.html, I’ll manage the rest with JS.

Also I want to be able to test this on my local machine. In this case, my URLs will be:

localhost/websites/myWebsite/products
localhost/websites/myWebsite/products/
localhost/websites/myWebsite/products/shoes
localhost/websites/myWebsite/products/shoes/
localhost/websites/myWebsite/products/shoes/adidas
localhost/websites/myWebsite/products/shoes/adidas/
localhost/websites/myWebsite/products/shoes/adidas/adidas-predator-20.3
localhost/websites/myWebsite/products/shoes/adidas/adidas-predator-20.3/

So I think we would need two htaccess files right?

2

Answers


  1. With your shown samples could you please try following, you should place .htaccess file on same level where folder products is present. Also place the products.html in same level too.

    RewriteEngine ON
    RewriteCond %{REQUEST_URI} ^/(?:products)/?((?:shoes)/?((?:adidas)/?((?:adidas-predator-20.3)/?)?)?)?$ [NC]
    RewriteRule ^(.*)$ products.html [L]
    

    If these are the only rules you will have then change L to END as an additional note here.

    Login or Signup to reply.
  2. You may use this single rule that will work on localhost and on production host as well:

    RewriteEngine On
    
    RewriteRule (?:^|/)products(?:/|$) products.html [L,NC]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search