无重定向执行PHP脚本
我在.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]
如果URL为https://example.com/mymya/mya/view.php :我想执行php script view.php。代码>,我希望
account/mya/view.php
被执行。
请告知我如何做。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的现有规则被用于其他目的,因为它们都不匹配所陈述的URL。
要内部重写请求
/mya/view.php
/accounts/mya/view.php
(如前所述),然后您将以下添加以下内容(或之后) )您的现有规则:使其更通用并重写请求
/m/< file>
to/eaccept/eaccect/< file>
,但仅当/eaccorp/< file>
,然后您可以在现有规则之前(或之后)进行以下操作:更新: regex part part
\。\。\ w { 2,5}
匹配,看起来像什么样,文件扩展名。 IE。一个点,其次是2至5个字字符。如果仅匹配.php
文件,则可以将其更改为\。php
以进行硬码.php
扩展。使用诸如前面的
rewriteCond
(条件)指令然后检查该文件是否在实际重写请求之前是否存在于预期的目标。没有此,无论目标文件是否存在,请求将无条件地重写。例如。/m/abc/xyz/does-not-exist.php
将内部重写为/accounts/abc/xyz/does-not-exist.php
,然后触发它a 404 稍后(有可能暴露帐户
子目录 - 取决于您处理404的方式)。这些规则与您现有规则相关的规则的顺序无关紧要,因为在提出文件请求时,正则是言论(
Rewriterule
模式)不会发生冲突(其中包含文件扩展名之前的点)。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: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: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 asregex101.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 theaccounts
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).