Below is the code from my .htaccess
file:
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
Below is what I want to search and replace:
-
https://example.com/video-17i8mp51/27628401/0/ok-ask-me-right
to
https://example.com/video-17i8mp51/ok-ask-me-right
-
https://example.com/search/full+movie?top&id=57448561
to
https://example.com/search/full+movie
-
This URL is in over 10k of my site content’s
https://anothersiteurl.com/search/full+movie
to
https://mysiteurl.com/search/full+movie
2
Answers
I was able to meet all three of your criteria with the following rules:
You can see them in action here.
I’m assuming these are static one-to-one redirects, as seemingly confirmed in comments.
Both the following rules should go after the first rule (the canonical HTTP to HTTPS and www to non-www redirect) and before the front-controller pattern.
Where the
$1
and$2
backreferences contain the captured subgroups from theRewriteRule
pattern, ie.video-17i8mp51
andok-ask-me-right
respectively. This simply saves repetition in theRewriteRule
substitution string.The
$0
backreference contains the full match of theRewriteRule
pattern (ie.search/full_movie
). Note that the literal+
needs to be backslash escaped in the regex to negate it’s special meaning in the regex.The
QSD
(Query String Discard) flag removes the original query string from the redirect response.You should not repeat the
RewriteEngine
directive.Note that these are currently 302 (temporary) redirects. If these are intended to be permanent then change to 301 but only after you have tested that they work as intended, to avoid potential caching issues.
This is not something you should be trying to do with
.htaccess
. If this URL appears in the site "content" then you need to modify the content of your pages before sending the response.(Technically, you can use mod_substitute to do this – to modify the response body – but really that would be a last resort.)
Aside: The
RewriteBase
directive is not being used here and can therefore be removed.Summary
Your resulting
.htaccess
file would then look like this: