Trying to rewrite a portion of the url to a specific php file

Viewed 35

i have 2 url's that do 2 different functions. I am trying to rewrite a URL so that if the url matches, it renders a specific php file.

https://example.com/adsupport/
https://example.com/adsupport/manage/

Each one of these url's use a unique php file.

Here's what I've tried and come close after reading plenty documentation.

RewriteRule ^adsupport(.*)$  application/views/support/admin.php [QSA]
RewriteRule ^adsupport/manage(.*)$  application/views/support/manage.php [QSA]

So this works only for the admin.php, however the manage part uses the same admin.php instead of the manage.php file.

What am i missing, please assist.

1 Answers

1st solution: With your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.

Please make sure either you try 1st solution OR 2nd solution at a time only.

RewriteEngine ON
RewriteRule ^adsupport/?$ application/views/support/admin.php [NC,QSA,L]
RewriteRule ^adsupport/([\w-]+)/?$ application/views/support/$1.php [NC,QSA,L]


2nd solution: OR in case you want to perform a check before Rewriting to file like if file is present or not then try following.

RewriteEngine ON
RewriteCond %{DOCUMENT_ROOT}/application/views/support/admin\.php -f [NC]
RewriteRule ^adsupport/?$ application/views/support/admin.php [NC,QSA,L]

RewriteCond %{DOCUMENT_ROOT}/application/views/support/$1\.php -f [NC]
RewriteRule ^adsupport/([\w-]+)/?$ application/views/support/$1.php [NC,QSA,L]
Related