.htaccess 使用查询参数重写URL

yfwxisqw  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(141)

我想重写一个特定的网址,并删除查询参数。然而,网上我似乎找不到一个解决方案,为我工作。我是相当新的。htacces文件和他们的工作。
目前我的文件,位于我的网站的主文件夹中:

ErrorDocument 404 /404.html

#Remove php extension
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

#Remove html extension
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.html -f 
RewriteRule ^(.*)$ $1.html

#Remove id query parameter
RewriteEngine on 
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^webhook/(.*)$ webhook.php?q=$1

所以正常的网址是:https://example.com/api/webhook.php?id=10
我想重写为:https://example.com/api/webhook/10
有没有人知道如何为这个特定的案例创建这个?提前感谢!
编辑:
在我的webhook.php文件中,我有这样的测试目的:

<?php echo $_GET['id']; ?>

当我转到url https://example.com/api/webhook.php?id=10时,我在浏览器中得到输出10。
当我转到url https://example.com/api/webhook/10
我收到此错误消息:* 内部服务器错误服务器遇到内部错误或配置错误,无法完成您的请求。*
在服务器日志中,我发现了以下错误:* 由于可能存在配置错误,请求超过了4个内部重定向得限制.如有必要,请使用“LimitInternalRecursion”增加限制.使用“LogLevel debug”获取回溯跟踪 *
似乎有太多的重写。

SOLVED我发现以下代码行解决了我的问题:

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

现在,当我转到https://examply.com/api/webhook/10时,它会加载带有query?id=10的webhook.php脚本。

uqdfh47h

uqdfh47h1#

RewriteEngine On 

RewriteRule ^api/webhook/([0-9]+)/?$ api/webhook.php?id=$1 [NC,L,QSA]

用法=这些规则将使您的url https://example.com/api/webhook.php?id=10变为https://example.com/api/webhook/10

相关问题