G
G
Gabe_B2021-05-11 20:22:01
htaccess
Gabe_B, 2021-05-11 20:22:01

How to write CNC rules for a self-written site?

Tell me, I want all links of the form:

site.ru/profile/
site.ru/users/
...
site.ru/любойтекст/
и т.д.

They accessed index.php?module=(anytext)
And at the same time, it was possible to write separate rules, such as:
site.ru/user/1/
Accessed index.php?module=user&id=1
, etc.

At the moment I wrote this:
Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)/$ index.php?module=$1 [L,QSA]
RewriteRule ^user/(\d+)*/$ index.php?module=user&id=$1 [L,QSA]


But it doesn't work correctly. They work separately, but not together.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2021-05-11
@rpsv

Don't reinvent the wheel, use ready-made libs: https://packagist.org/?query=router

D
dodo512, 2021-05-11
@dodo512

Just swap the rules and ^user/(\d+)put first.

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^user/(\d+)/$ index.php?module=user&id=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?module=$1 [L,QSA]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question