下面是我的.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
下面是我要搜索和替换的内容:
https://example.com/video-17i8mp51/27628401/0/ok-ask-me-right
到https://example.com/video-17i8mp51/ok-ask-me-right
https://example.com/search/full+movie?top&id=57448561
到https://example.com/search/full+movie
https://anothersiteurl.com/search/full+movie
到https://mysiteurl.com/search/full+movie
的10k以上发布于 2022-11-02 01:20:22
我假设这些是静态的一对一的重定向,这一点似乎在评论中得到了证实。
以下两条规则都应该遵循第一个规则(规范的HTTP到HTTPS和www到非www重定向)和前面的控制器模式。
https://example.com/video-17i8mp51/27628401/0/ok-ask-me-right
到https://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-17i8mp51
和ok-ask-me-right
。这只会节省RewriteRule
替换字符串中的重复。
https://example.com/search/full+movie?top&id=57448561
到https://example.com/search/full+movie
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,但只在您测试了它们是否按预期工作之后,以避免潜在的缓存问题。
https://anothersiteurl.com/search/full+movie
到https://mysiteurl.com/search/full+movie
这不是你应该尝试用.htaccess
做的事情。如果这个URL出现在站点" content“中,那么在发送响应之前,您需要修改页面的内容。
(从技术上讲,您可以使用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
发布于 2022-11-01 23:39:34
通过以下规则,我能够满足你的所有三项标准:
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]
您可以在行动这里中看到它们。
https://stackoverflow.com/questions/74278574
复制相似问题