重写除URL的特定部分以外的所有内容

发布于 2025-02-01 02:06:28 字数 727 浏览 3 评论 0原文

我有此用户生成的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我是有多爱你 2025-02-08 02:06:28
 重新写入 ^([ ^ /]*)\。html $ /watch.php?name = $ 1 [l]
 

不确定为什么您要与.html结束的URL匹配时,当您的URL看起来像/I9AN9O时。

您需要做类似的事情:

RewriteRule ^\w+$ watch.php?name=$0.mp4 [L]

\ w是一个速记字符类,仅匹配上/小写字母,数字和下划线。因此,不匹配包含点的URL(以避免与watch.php的实际文件发生冲突)或包含多个路径段(文件夹)的URL。使用更通用的正则[^/]*(如您的原始示例中)的问题是,它也可能匹配watch.php,创建一个无尽的循环。

$ 0 BackReference包含由Rewriterule staters 匹配的整个URL路径。

您应该链接到html源中的/i9an9o的URL。

如果要更改现有的URL结构,则您还需要将表单/watch.php?name = i9an9o.mp4的“旧”请求重定向到新URL。此重定向将需要在上述重写之前进行。

完整的.htaccess文件将是以下内容:

Options -MultiViews

RewriteEngine On

# Redirect direct requests to "/watch.php?name=<name>.mp4" to "/<name>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^name=(\w+)\.mp4
RewriteRule ^watch\.php$ /%1 [QSD,R=301,L]

# Rewrite requests of the form "/<name>" to "watch.php?name=<name>.mp4"
RewriteRule ^\w+$ watch.php?name=$0.mp4 [L]

其中第一个%1在第一个Rewriterule中是对捕获的Subpattern的反向。 condpattern 。 IE。 i9an9o name = i9an9o.mp4的一部分。

QSD标志对于 dostard dost 从请求中的原始查询字符串。

首先先用302(临时)重定向测试,然后更改为301(永久),以避免潜在的缓存问题。

RewriteRule ^([^/]*)\.html$ /watch.php?name=$1 [L]

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:

RewriteRule ^\w+$ watch.php?name=$0.mp4 [L]

\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 like watch.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 match watch.php as well, creating an endless loop.

The $0 backreference contains the entire URL-path that is matched by the RewriteRule 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:

Options -MultiViews

RewriteEngine On

# Redirect direct requests to "/watch.php?name=<name>.mp4" to "/<name>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^name=(\w+)\.mp4
RewriteRule ^watch\.php$ /%1 [QSD,R=301,L]

# Rewrite requests of the form "/<name>" to "watch.php?name=<name>.mp4"
RewriteRule ^\w+$ watch.php?name=$0.mp4 [L]

Where %1 in the first RewriteRule is a backreference to the captured subpattern in the preceding CondPattern. ie. the I9an9O part of name=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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文