Home > Enterprise >  How to search and replace url using .htaccess (regex)
How to search and replace url using .htaccess (regex)

Time:11-02

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

CodePudding user response:

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.

CodePudding user response:

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
  • Related