0

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:

  1. https://example.com/video-17i8mp51/27628401/0/ok-ask-me-right to https://example.com/video-17i8mp51/ok-ask-me-right

  2. https://example.com/search/full+movie?top&id=57448561 to https://example.com/search/full+movie

  3. This URL is in over 10k of my site content's https://anothersiteurl.com/search/full+movie to https://mysiteurl.com/search/full+movie

6
  • "3 This url is in over 10k of my site content's" - This isn't something you should be modifying with .htaccess. #1 and #2 look like two static redirects - is that the case? What have you tried? Commented Nov 1, 2022 at 17:11
  • I fail to see any attempt in that file to rewrite requests according to what you write that you want to achieve ... Commented Nov 1, 2022 at 18:05
  • @MrWhite I totally do not know where to start from, please help in any way you can! Commented Nov 1, 2022 at 19:11
  • There doesn't appear to be any "search and replace" here? You seem to be just redirecting from one URL to another - is that correct? Commented Nov 1, 2022 at 19:37
  • @MrWhite yes "redirecting from one URL to another" Commented Nov 1, 2022 at 20:36

2 Answers 2

1

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.

  1. https://example.com/video-17i8mp51/27628401/0/ok-ask-me-right to https://example.com/video-17i8mp51/ok-ask-me-right
RewriteRule ^(video-17i8mp51)/27628401/0/(ok-ask-me-right)$ /$1/$2 [R=302,L]

Where the $1 and $2 backreferences contain the captured subgroups from the RewriteRule pattern, ie. video-17i8mp51 and ok-ask-me-right respectively. This simply saves repetition in the RewriteRule substitution string.

  1. https://example.com/search/full+movie?top&id=57448561 to https://example.com/search/full+movie
RewriteCond %{QUERY_STRING} ^top&id=57448561$
RewriteRule ^search/full\+movie$ /$0 [QSD,R=302,L]

The $0 backreference contains the full match of the RewriteRule 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.

  1. This url is in over 10k of my site content's
    https://anothersiteurl.com/search/full+movie to https://mysiteurl.com/search/full+movie

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:

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
Sign up to request clarification or add additional context in comments.

4 Comments

RewriteCond %{QUERY_STRING} ^top&id=57448561$ RewriteRule ^search/full\+movie$ /$0 [QSD,R=302,L] @MrWhite you do focus only on example URL, I want generally do this in all url containing ?top&id=numbers
@ZamaniVogue Because your examples are static, single URLs - you've not stated what the variable parts are. The question simply asks how to redirect from one URL to the other. I tried to seek clarification in comments, and you simply repeated my question that you are "redirecting from one URL to another". You need to state which parts in the URLs are variable and how they are variable, ie. containing only numbers, letters, etc. Please update your question with the additional information, otherwise we are guessing what your intentions are.
@ZamaniVogue In the 2nd example (the one quoted in your comment), there was no indication that the URL-path (ie. /search/full+movie) was variable. Is it /search/<movie-name> or literally /<anything>? Is video-17i8mp51 (1st example) also variable? Is it video-<digits-and-lowercase-letters>? Is the last path segment (ie. ok-ask-me-right) in that example also variable? If so, what characters can it contain? Or is it a specific video URL you are wanting to redirect? You need to be specific.
I want to generally redirect all the url containing &id=anynumbers
0

I was able to meet all three of your criteria with the following rules:

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]

You can see them in action here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.