【问题标题】:Rewrite rule regexp重写规则正则表达式
【发布时间】:2016-02-10 02:20:43
【问题描述】:

我想问你们,因为我的重写规则有一个问题,我不知道如何写出好的规则。 我有这个规则:

RewriteRule ^wp-content/uploads/(.*[^(.js|.swf)])$ authenticate.php?file=$1

每当有人尝试在wp-content 上传目录中打开某些内容并且我想将文件名发送到 php 时,我想做的是将用户重定向到authenticate.php

例如:

http://domain.tld/wp-content/uploads/2015/11/something.pdf

重定向到authenticate.php?file=something.pdf

但不幸的是,我的正则表达式坏了。有人可以帮我吗?

非常感谢!

【问题讨论】:

    标签: regex wordpress .htaccess


    【解决方案1】:

    在您的 .htaccess 中尝试:

    RewriteCond %{REQUEST_URI} !(?:js|swf)$ [NC]
    RewriteRule ^wp-content/uploads/(.+)$ authenticate.php?file=$1 [NC,L]
    

    对于http://domain.tld/wp-content/uploads/2015/11/something.pdf,结果:http://domain.tld/authenticate.php?file=2015/11/something.pdf

    【讨论】:

    • 如果您不想要/2015/11/,请指定格式是否始终存在并且始终采用/YYYY/MM/的格式
    【解决方案2】:

    尝试使用带有负前瞻的正则表达式:

    ^.*?wp-content\/uploads\/.*?\.(?!js$|swf$)[^.]+$
    

    以下网址将匹配正则表达式:

    http://cpsma.ie/wp-content/uploads/2015/09/CPSMA-Newsletter-No-35-Sept-2015-2.pdf
    

    但是,以.js.swf 结尾的类似网址将匹配。因此,以下两个 URL 匹配正则表达式:

    http://domain.tld/wp-content/uploads/2015/10/javascript.js
    http://domain.tld/wp-content/uploads/2015/02/shockwavefile.swf
    

    你可以在这里测试这个正则表达式:

    Regex101

    【讨论】:

    • 您好!感谢您的回答。我试过了,但不幸的是它不起作用。这是链接cpsma.ie/wp-content/uploads/2015/09/… 但它显示了pdf内容。这是 htaccess [code] # BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule 。 /index.php [L] RewriteRule ^http.*?wp-content\/uploads\/.*?\.(?!js$|swf$)[^.]+$ auth.php?file=$1 IfModule> # END WordPress [/code]
    • 我更新了我的答案。我假设你所有的 URL 都以 http 开头,但显然情况并非如此。
    • 我认为重定向不起作用,因为它根本没有被触发。您的正则表达式很好,但是任何时候我调用该页面时都没有任何反应。 :/ RewriteRule ^.*?wp-content\/uploads\/.*?\.(?!js$|swf$)[^.]+$ auth.php?file=$1
    猜你喜欢
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 2011-01-15
    • 2011-10-13
    相关资源
    最近更新 更多