无重定向执行PHP脚本

发布于 2025-02-12 03:34:50 字数 458 浏览 1 评论 0原文

我在.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似最初 2025-02-19 03:34:50

我认为您的现有规则被用于其他目的,因为它们都不匹配所陈述的URL。

要内部重写请求/mya/view.php /accounts/mya/view.php(如前所述),然后您将以下添加以下内容(或之后) )您的现有规则:

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

使其更通用并重写请求/m/< file> to /eaccept/eaccect/< file>,但仅当/eaccorp/< file>,然后您可以在现有规则之前(或之后)进行以下操作:

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

更新: 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:

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文