-1

I have 2 simple PHP files:

product.php
product-options.php

And in my htaccess file i have:

RewriteEngine On
RewriteRule ^product$ product.php [L]
RewriteRule ^options$ product-options.php [L]
RewriteRule ^product-([^/]+)/?$ product.php?id=$1 [L]
RewriteRule ^options-([^/]+)/?$ product-options.php?id=$1 [L]

Everything works fine when i call "mysite.com/product" or "mysite.com/product-MYID"

But when i call "mysite.com/options" or "mysite.com/option-MYID" i see the correct url but the content of the file is "product" / "product-MYID" !!! Why???

PS: i need that both files are accessibile with and without the GET ?id=ID

2
  • It's just because you redirect -> options -> product-options.php... -> product-... -> product.php...
    – Croises
    Nov 6, 2014 at 13:12
  • The easier for you is to change product-options.php to options.php or to put .php file in a folder
    – Croises
    Nov 6, 2014 at 13:17

2 Answers 2

1

Or you can use:

RewriteEngine On
RewriteRule ^product$ product.php [L]
RewriteRule ^options$ product-options.php [L]
RewriteRule ^product-([^/\.]+)/?$ product.php?id=$1 [L]
RewriteRule ^options-([^/\.]+)/?$ product-options.php?id=$1 [L]

because, I think, you don't use . in your product-... or option-...

1

That is because your rules are executing 2nd time as regex is:

^product-([^/]+)/?$

Which matches /product-MYID URI as well as rewritten URI: product-options.php

In order to avoid this you can use these rules:

RewriteEngine On

# If the request is for a valid directory or file then skip rewrite rules
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.