r/PHPhelp • u/SatisfactionVast5052 • Nov 12 '25
Solved Problem with HTACCESS
Hello,
I have this HTACCESS :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)(?:\/([A-Za-z0-9-]+))(?:\/([A-Za-z0-9-]+))?\/?$ index.php?controller=$1&publish_type=$2&action=$3 [NC,L]When i type :
I I have error 404
But if i write :
http://monsite.com/tsetvar/tse
or
http://monsite.com/tsetvar/tse/tes
All is ok.
Why only parameter 1 don't work please.
THX
2
Upvotes
3
u/dave8271 Nov 12 '25
Understanding and fixing regular expressions is the kind of problem ChatGPT genuinely excels at. Literally just go on chatgpt.com and paste your post in there, it will tell you exactly how to fix it.
2
u/allen_jb Nov 12 '25
If you don't need to be so specific about what requests PHP handles, consider using FallbackResource, and handling the URL routing entirely in the PHP code.
This could eliminate the need for regex altogether here - simply explode the request URI on
/.1
u/norpan83 Nov 13 '25 edited Nov 13 '25
In apache you need to allow .htaccess to overwrite default settings.
The default in /etc/apache/apache2.conf
is:
The key here is AllowOverride None,
This blocks .htaccess files from overwriting default settings.
So you need to allo overwrite,
for example you can add in /etc/apache2/sites-enabled/000-default.conf
1
u/rmb32 Nov 17 '25
Because the second part of your regex isn’t optional. It will insist that you need part 1 and part 2 always (even though part 2 is non-capturing). Part 3 is optional because you put a question mark after it.