首页 > 解决方案 > htaccess 正则表达式不附加获取值

问题描述

首先,为这个相当直率的标题道歉,我有点初学者htaccess,所以请多多包涵。我正在尝试为我网站上的图像创建一个小的防热链接脚本(您可以在此处查看格式更好的正则表达式:

Options -MultiViews

RewriteEngine On
# RewriteBase /

# only allow rewriting to paths that dont exist
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^ruid\.php(?:/t(\d))?/r(\d{2,4})x(\d{2,4})?/([\w-]+)/?$ ruid.php?type=$1&width=$2&height=$3&image=$4 [L,NC,QSA]

# no php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

例如,如果我去

www.example.com/ruid.php/t1/r400x500/313ef70

它应该指向

www.example.com/ruid.php?type=1&width=400&height=500&image=313ef70

但是,当我var_dump输入$_GET数据时ruid.php,我收到一个空数组:

// returns array(0){}
die(var_dump($_GET));

在搜索了一会儿之后,试图找到合适的标志和语法来htaccess重写,我没有找到任何可以解决我的问题的方法。

感谢所有帮助。

标签: regexapache.htaccess

解决方案


您可以将此规则与经过调整的正则表达式一起使用:

Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine On
RewriteBase /

# only allow rewriting to paths that dont exist
RewriteRule ^ruid\.php(?:/t(\d))?/r(\d{2,4})x(\d{2,4})?/([\w-]+)/?$ ruid.php?type=$1&width=$2&height=$3&image=$4 [L,NC,QSA]

# no php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

更新的正则表达式演示

  • +最后一个参数使用量词
  • 使用\w代替[a-zA-Z0-9_]
  • 无需/逃避mod_rewrite规则

推荐阅读