Home > OS >  Execute PHP script without Redirect
Execute PHP script without Redirect

Time:07-02

I have below Rewrite rule in .htaccess:

# /m/yyy rule
RewriteRule ^m/([\w-] )/?$ accounts/$1/index.php [L,NC]
# /m/yyy/abc rule
RewriteRule ^m/([\w-] )/([\w-] )$ accounts/$1/$2.php [L,NC]
# /m/yyy/abc/ rule
RewriteRule ^m/([\w-] )/([\w-] )/$ accounts/$1/$2/index.php [L,NC]

I want to execute the PHP script view.php if the URL is https://example.com/m/mya/view.php, I expect accounts/mya/view.php to be executed.

Please advise how I can do.

CodePudding user response:

I assume your existing rules are being used for other purposes, since none of them will match the stated URL.

To internally rewrite the request /m/mya/view.php to /accounts/mya/view.php (as stated) then you would add the following before (or after) your existing rules:

RewriteRule ^m/(mya/view\.php)$ accounts/$1 [L]

To make this more generic and rewrite the request /m/<file> to /accounts/<file>, but only if /accounts/<file> exists then you can do something like the following instead before (or after) your existing rules:

RewriteCond %{DOCUMENT_ROOT}/accounts/$1 -f
RewriteRule ^m/([\w/-] \.\w{2,5})$ accounts/$1 [L]

The order of these rules in relation to your existing rules as posted does not matter.

  • Related