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

i5desfxk  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(123)

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

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
    1.此URL在我的网站内容的https://anothersiteurl.com/search/full+moviehttps://mysiteurl.com/search/full+movie中超过10k
iaqfqrcu

iaqfqrcu1#

我假设这些都是静态的一对一重定向,似乎在评论中确认。
以下两个规则都应在第一个规则(规范HTTP到HTTPS和www到非www重定向)* 之后 *,在前端控制器模式 * 之前 *。

  1. https://example.com/video-17i8mp51/27628401/0/ok-ask-me-righthttps://example.com/video-17i8mp51/ok-ask-me-right
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
RewriteCond %{QUERY_STRING} ^top&id=57448561$
RewriteRule ^search/full\+movie$ /$0 [QSD,R=302,L]

$0反向引用包含RewriteRulepattern 的完全匹配(即search/full_movie)。请注意,文字+需要在正则表达式中进行反斜杠转义,以否定其在正则表达式中的特殊含义。
QSD(放弃查询字符串)标志从重定向响应中删除原始查询字符串。
不应重复RewriteEngine指令。
请注意,这些当前是302(临时)重定向。如果这些重定向是永久性的,则更改为301,但只有在测试它们是否按预期工作后,才能更改,以避免潜在的缓存问题。
1.此URL在我的网站内容的10k以上
https://anothersiteurl.com/search/full+moviehttps://mysiteurl.com/search/full+movie
这不是你应该尝试用.htaccess做的事情。如果这个URL出现在网站的“内容”中,那么你需要在发送响应之前修改页面的内容。
(从技术上讲,可以使用mod_substitute来修改响应主体,但这实际上是最后的手段。)

  • 旁白:* 此处未使用RewriteBase指令,因此可以删除。

摘要

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

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
wmtdaxz3

wmtdaxz32#

我能够满足您的所有三个标准与以下规则:

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]

你可以看到他们的行动here

相关问题