重写除URL的特定部分以外的所有内容
我有此用户生成的URL:https://example.com/watch.php?name=i9an9o.mp4
我要实现的目标是抓住inbetween
name =
和.mp4
(即i9an9o
)
,这就是我希望URL的外观:https://example.com /i9an9o
我尝试将此代码放入.htaccess
:
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /watch.php?name=$1 [L]
不幸的是,我只能删除i9an9o
的零件i9an9o
之后。我使用了此在线mod重写工具: https://www.generateit.generateit.net/mod-----------重写/index.php
使用它后,结果是:https://example.com/i9an9o.mp4.html
我在做什么错?
I have this user-generated URL: https://example.com/watch.php?name=I9an9O.mp4
What I'm trying to achieve is to grab the part that is inbetween name=
and .mp4
(ie. I9an9O
)
And this is how I want the URL to look like: https://example.com/I9an9O
I have tried putting this code into .htaccess
:
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /watch.php?name=$1 [L]
Unfortunately, I was only able to remove the part that was in front of I9an9O
but not the extensions after I9an9O
. I used this online Mod Rewrite Tool: https://www.generateit.net/mod-rewrite/index.php
After using it, the result was: https://example.com/I9an9O.mp4.html
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定为什么您要与
.html
结束的URL匹配时,当您的URL看起来像/I9AN9O
时。您需要做类似的事情:
\ w
是一个速记字符类,仅匹配上/小写字母,数字和下划线。因此,不匹配包含点的URL(以避免与watch.php
的实际文件发生冲突)或包含多个路径段(文件夹)的URL。使用更通用的正则[^/]*
(如您的原始示例中)的问题是,它也可能匹配watch.php
,创建一个无尽的循环。$ 0
BackReference包含由Rewriterule
staters 匹配的整个URL路径。您应该链接到html源中的
/i9an9o
的URL。如果要更改现有的URL结构,则您还需要将表单
/watch.php?name = i9an9o.mp4
的“旧”请求重定向到新URL。此重定向将需要在上述重写之前进行。完整的
.htaccess
文件将是以下内容:其中第一个
%1
在第一个Rewriterule
中是对捕获的Subpattern的反向。 condpattern 。 IE。i9an9o
name = i9an9o.mp4
的一部分。QSD
标志对于 dostard dost 从请求中的原始查询字符串。首先先用302(临时)重定向测试,然后更改为301(永久),以避免潜在的缓存问题。
Not sure why you are matching URLs that end with
.html
when your URLs should look like/I9an9O
.You would need to do something like this instead:
\w
is a shorthand character class that matches upper/lowercase letters, digits and underscores only. So won't match URLs that contain dots (to avoid conflicts with actual files likewatch.php
) or URLs that contain multiple path segments (folders). The problem with using the more generic regex[^/]*
(as in your original example) is that it would potentially matchwatch.php
as well, creating an endless loop.The
$0
backreference contains the entire URL-path that is matched by theRewriteRule
pattern.You should be linking to URLs of the form
/I9an9O
in your HTML source.If you are changing an existing URL structure then you will also need to redirect "old" requests of the form
/watch.php?name=I9an9O.mp4
to the new URL. This redirect will need to go before the above rewrite.The complete
.htaccess
file would then be something like the following:Where
%1
in the firstRewriteRule
is a backreference to the captured subpattern in the preceding CondPattern. ie. theI9an9O
part ofname=I9an9O.mp4
.The
QSD
flag is necessary to discard the original query string from the request.Test first with 302 (temporary) redirects before changing to a 301 (permanent) in order to avoid potential caching issues.