How do I make parameters optional in my htaccess rewrite?

若如初见. 提交于 2020-03-05 02:06:45

问题


I want all of the following rewrites to be possible:

https://website.com/CAT/api/translation/tasks/get/3/4x
https://website.com/CAT/api/translation/tasks/get.php?param1=3&param2=4x

https://website.com/CAT/api/glossary/folder/anotherfolder/put/6
https://website.com/CAT/api/glossary/folder/anotherfolder/put.php?param1=6

The first url works fine. However, I want the amount of parameters to be optional. If I remove a parameter: https://website.com/CAT/api/translation/tasks/get/3/
I get the error The requested URL /CAT/api.php was not found on this server.

For some reason, only having 1 parameter causes the rewrite to change the folder api to api.php instead of the file get to get.php.

Here is my .htaccess which is located in the CAT directory.

Options -MultiViews
RewriteEngine On
RewriteBase /CAT/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/([^/]+)/([^/]+)/?$ $1.php?param1=$2&param2=$3 [QSA,NC,L]

来源:https://stackoverflow.com/questions/60230378/how-do-i-make-parameters-optional-in-my-htaccess-rewrite

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!