0

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.

2
  • Could you please confirm which url you are hitting and which url you want it to rewrite or redirect(if any in browser)? Thank you. Commented Jul 1, 2022 at 11:35
  • "Execute PHP script without Rewrite" - I've "corrected" your title, as I assume you meant "Redirect", not "Rewrite". The only way you can do what you are asking is with an internal rewrite. Commented Jul 1, 2022 at 16:26

1 Answer 1

1

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]

UPDATE: The regex part \.\w{2,5} matches, what looks-like, a file extension. ie. a dot followed by between 2 and 5 word characters. If you are only matching .php files then you can change this to \.php to hardcode the .php extension. Use a regex testing tool such as regex101.com to test this and get a detailed explanation of the regex (this is not unique to Apache - Apache uses the same regex engine as PHP and other languages, ie. PCRE).

The preceding RewriteCond (condition) directive then checks that this file exists at the intended destination before actually rewriting the request. Without this, the request is rewritten unconditionally, regardless of whether the target file exists or not. eg. /m/abc/xyz/does-not-exist.php would be internally rewritten to /accounts/abc/xyz/does-not-exist.php which then triggers a 404 later (potentially exposing the accounts subdirectory - depending on how you are handling your 404s).

The order of these rules in relation to your existing rules as posted does not matter since the regex (RewriteRule patterns) do not conflict when making a request for a file (that contains a dot before the file extension).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @MrWhite, I tested but know I idea to the RewriteCond and the w{2,5} mean, here is my test htaccess.madewithlove.com?share=af545ff4-0a53-45db-9322-afa4253…
@Kelvin I've updated my answer with more details on those points. You cannot use the MWL .htaccess tester to test this properly since it has no knowledge of the filesystem (and does not support the -f file check).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.