Europeuser Europeuser - 6 months ago 23
PHP Question

.htaccess to load index in dynamic folder name

I am having problem with .htaccess to load index in dynamic folder name.
Here is my directory structure:

root/products/ -> this is constant folder name


instead of using GET for product urls:

root/products/index.php?product=my-product-url


I want to put the product url after products folder and to look like:

root/products/my-product-url


My .htaccess file is in products folder and has this code:

RewriteBase /products/
RewriteRule ^index\.php$ - [L,NC]
RewriteRule . index.php [L]


but it doesn't work.
It actually loads the index file but in the console I can see it is looding js file over 1000 times if I don't stop the page.. It looks like is reloading the page so many times and adds I suppose timestamp at the end of the js file like:

GET http://www.mysiteexaple.com/js/jalerts/jquery.js?_=1464945951342

Can you help me please and show me what is wrong? thanks !

Answer

Inside /products/.htaccess you can use these rules:

RewriteEngine On
RewriteBase /products/

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/?$ index.php?product=$1 [L,QSA]

RewriteRule ^[^/]+/([^/]+)/?$ index.php?product=$1 [L,QSA]