首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用.htaccess搜索和替换url (regex)

如何使用.htaccess搜索和替换url (regex)
EN

Stack Overflow用户
提问于 2022-11-01 15:58:50
回答 2查看 59关注 0票数 1

下面是我的.htaccess文件中的代码:

代码语言:javascript
运行
复制
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

下面是我要搜索和替换的内容:

  1. https://example.com/video-17i8mp51/27628401/0/ok-ask-me-righthttps://example.com/video-17i8mp51/ok-ask-me-right
  2. https://example.com/search/full+movie?top&id=57448561https://example.com/search/full+movie
  3. 此网址位于我的站点内容的https://anothersiteurl.com/search/full+moviehttps://mysiteurl.com/search/full+movie的10k以上
EN

回答 2

Stack Overflow用户

发布于 2022-11-02 01:20:22

我假设这些是静态的一对一的重定向,这一点似乎在评论中得到了证实。

以下两条规则都应该遵循第一个规则(规范的HTTP到HTTPS和www到非www重定向)和前面的控制器模式。

  1. https://example.com/video-17i8mp51/27628401/0/ok-ask-me-righthttps://example.com/video-17i8mp51/ok-ask-me-right
代码语言:javascript
运行
复制
RewriteRule ^(video-17i8mp51)/27628401/0/(ok-ask-me-right)$ /$1/$2 [R=302,L]

其中,$1$2反向引用包含从RewriteRule模式捕获的子组,即。分别为video-17i8mp51ok-ask-me-right。这只会节省RewriteRule替换字符串中的重复。

  1. https://example.com/search/full+movie?top&id=57448561https://example.com/search/full+movie
代码语言:javascript
运行
复制
RewriteCond %{QUERY_STRING} ^top&id=57448561$
RewriteRule ^search/full\+movie$ /$0 [QSD,R=302,L]

$0反向引用包含RewriteRule模式的完全匹配(即。search/full_movie)。注意,文本+需要反斜杠,在regex中转义,以否定它在regex中的特殊意义。

QSD (查询字符串丢弃)标志从重定向响应中删除原始查询字符串。

您不应该重复RewriteEngine指令。

请注意,这些当前是302 (临时)重定向。如果它们是永久的,那么更改为301,但只在您测试了它们是否按预期工作之后,以避免潜在的缓存问题。

  1. 这个网址在我网站内容的10k以上 https://anothersiteurl.com/search/full+moviehttps://mysiteurl.com/search/full+movie

这不是你应该尝试用.htaccess做的事情。如果这个URL出现在站点" content“中,那么在发送响应之前,您需要修改页面的内容。

(从技术上讲,您可以使用mod_substitute来做这件事--修改响应体--但实际上这是最后的手段。)

旁白:这里没有使用RewriteBase指令,因此可以删除。

摘要

生成的.htaccess文件如下所示:

代码语言:javascript
运行
复制
RewriteEngine On

# Canonical redirect (HTTP to HTTPS and www to non-www)
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

# Point#1
RewriteRule ^(video-17i8mp51)/27628401/0/(ok-ask-me-right)$ /$1/$2 [R=302,L]

# Point#2
RewriteCond %{QUERY_STRING} ^top&id=57448561$
RewriteRule ^search/full\+movie$ /$0 [QSD,R=302,L]

# Front-controller pattern
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
票数 1
EN

Stack Overflow用户

发布于 2022-11-01 23:39:34

通过以下规则,我能够满足你的所有三项标准:

代码语言:javascript
运行
复制
RewriteEngine On
RewriteBase /

# First request:
# Convert https://example.com/video-17i8mp51/27628401/0/ok-ask-me-right to
#         https://example.com/video-17i8mp51/ok-ask-me-right
RewriteRule ^(video-[^/]+)/.+/(.+)/?$ $1/$2 [L]

# Second request:
# Convert https://example.com/search/full+movie?top&id=57448561 to
#         https://example.com/search/full+movie
RewriteRule ^ %{REQUEST_URI}?

# Third request:
# Convert https://anothersiteurl.com/search/full+movie to
#         https://mysiteurl.com/search/full+movie
RewriteRule ^(.*)$ https://mysiteurl.com/$1 [R=301,L]

您可以在行动这里中看到它们。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74278574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档