【问题标题】:mod_rewrite : only forward parameter that starts with number 4mod_rewrite :仅以数字 4 开头的转发参数
【发布时间】:2009-09-26 00:03:18
【问题描述】:

我正在尝试修改以下重写条件,以便只有以数字 4 开头的字符串作为变量重定向到 process.php 页面:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /process.php?variable=$1 [L]

所以下面

http://www.domain.com/4shopping

将被映射到

http://www.domain.com/process.php?variable=4shopping

但以下不会

http://www.domain.com/shopping

原因是 /shopping 只是一个普通页面(由 Wordpress 提供服务),但 /4shopping 是一个应该作为变量映射到 /process.php 的代码。

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    这样就可以了:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(4[^/]*)$ /process.php?variable=$1 [L]
    

    正则表达式的意思是:

    ^      Start of string
    (      Start capturing group (for $1)
     4     A literal "4"
     [^/]* Any character except "/", 0 or more times
    )      End capturing group
    $      End of string
    

    【讨论】:

    • 感谢您的出色解释。如果不满足第 4 个条件,您是否知道如何切换回辅助条件,对于 Wordpress 来说是: RewriteRule 。 /index.php [L] 如果我运行 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(4[^/]*)$ /process.php?variable= 我只会得到错误1 美元 [L] 重写规则。 /index.php [L]
    • 抱歉,这有点乱。我想说的是,我需要一些“如果 4 存在则 /process.php?variable=$1 [L],否则 /index.php [L]
    猜你喜欢
    • 2020-09-24
    • 2013-06-11
    • 2023-02-08
    • 2017-12-26
    • 2021-12-23
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多