nick
nick

Reputation: 3239

Mod rewrite redirect every subdirectory to the same file

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?

Upvotes: 1

Views: 151

Answers (2)

anubhava
anubhava

Reputation: 785128

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]

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133508

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.

Upvotes: 0

Related Questions