2

The following is my current .htaccess file. If I open https://example.com/test and it doesn't end with one of the endings specified in line 2, it will open https://example.com/index.php?url=test

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !\.(css|js|png|jpg|php|svg|gif|webm|webp|eot|ttf|woff|woff2)
RewriteRule ^([^/]+)/? index.php?url=$1 [L,QSA]

However, if I try to open https://example.com/something/test, it just opens ?url=something instead of ?url=something/test. How can I achieve this? I have absolutely no knowledge about regex stuff

1

The regex ^([^/]+)/? only captures the first path segment, ie. [^/]+ matches 1 or more non-slash characters. If you wish to match the entire URL-path then use (.+) (to match 1 or more any characters). However, you can be more restrictive if you only want to match say 1 or 2 path segments?

and it doesn't end with one of the endings specified in line 2

That condition doesn't specifically check that the URL "ends" with one of those extensions, it checks that one of those extensions are contained within the filesystem path that the URL maps to. You need an end-of-string anchor $ at the end of the regex. And to specifically check the URL, you should check against the REQUEST_URI server variable.

So, this becomes:

RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|php|svg|gif|webm|webp|eot|ttf|woff|woff2)$
RewriteRule (.+) index.php?url=$1 [L,QSA]
  • 1
    That htaccess file works. Thank you very much. – OfficialCRUGG Oct 14 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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