Rick Joe Rick Joe - 3 months ago 9
PHP Question

how to open mypage.com/link-page in htaccess without affect other page?

I have this code to open

mywebsite.com/any-post-name
:

RewriteRule ^([a-zA-Z0-9_-]+)$ home.php?id=$1 [L,QSA]


the problem is, if anyone types: mywebsite.com/any-post-name
/
the page wont open.
So I add:

RewriteRule ^([a-zA-Z0-9_/-]+)$ home.php?id=$1 [L,QSA]


now, every other page goes to home.php too, for example, I have category:
mypage.com/category/name-category -> will come to home.php

RewriteRule ^category/([^(.*)].+)/(\d+)/?$ category.php?categoria=$1&id=$2


how to solve this?

Answer

Putting the / in the character class allows it multiple times because of the quantifier (+). If you move that to the end of the regex and make it optional it should work for you.

^([a-zA-Z0-9_-]+)/?$

Demo: https://regex101.com/r/jA0oZ1/3

or

^([^/]+)/?$

Demo: https://regex101.com/r/jA0oZ1/4

Comments