regex - 用 htaccess (php) 重写 URL

标签 regex .htaccess mod-rewrite url-rewriting

我对 .htaccess 不是最好的,所以我希望这里有人可以提供帮助。我正在尝试重写 URL 以使其看起来更干净。
原网址:domain.com/admin/users?userid=<id>我要的是:domain.com/admin/users/<id>我试过这个代码,但我得到了一个 404:

RewriteRule ^admin/users/([a-zA-Z0-9_-]+)$ users.php?userid=$1
RewriteRule ^admin/users/([a-zA-Z0-9_-]+)/$ users.php?userid=$1

最佳答案

重写时省略了目录。字符类也可以简化,尾部斜杠可以是可选的:

RewriteRule ^admin/users/([-\w]+)/?$ admin/users.php?userid=$1
\w 的详细记录可以在这里找到,http://regular-expressions.info/shorthand.html .简而言之:

\w stands for “word character”. It always matches the ASCII characters [A-Za-z0-9_]


也可以使用regex101,http://regex101.com/r/Px2vTE/1

关于regex - 用 htaccess (php) 重写 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68851525/

相关文章:

sql-server - notepad++正则表达式转换查询

.htaccess - 在安全域上强制将传出链接作为 http 与 https? (htaccess?)

.htaccess - 重写基本: from specific url to another specific url

java - String.split() 返回一个带有附加空值的数组

php - 使用正则表达式从地址中提取数字

apache - .htaccess 重定向添加哈希,同时保留查询字符串

php - Apache mod_rewrite : what am I doing wrong?

apache - htaccess 在我尝试打开页面时显示错误

Java 拆分正则表达式

php - 将 url 重写为干净的 url