rewrite rule forward one url to another wordpress

I’m using wordpress

I would like to forward one url to another preferably via wordpress like add_rewrite_rule() but as I know it is not possible. (Because it will be much easier.)

Read More

I have few cases that I want to match with same rule

I would like to forward

http://dom.com/sale/product to http://dom.com/product

http://dom.com/sale/hierachicalproduct/product to http://dom.com/hierachicalproduct/product

http://localhost/dom.com/sale/product to http://localhost/dom.com/product

http://localhost/dom.com/sale/hierachicalproduct/product to http://localhost/dom.com/hierachicalproduct/product

I want to remove “/sale” rest will be same.

I do not want to type single rule for every case.

This is the code that I used but problem is this code forwarding like

http://localhost/dom.com/sale/product to http://localhost/product
it is skipping /dom.com side

RewriteRule ^sale/(.+)$ /$1 [R=301,L]

Related posts

3 comments

  1. This code should work from .htaccess in http://localhost/dom.com/ on localhost and http://dom.com/ on production host.

    RewriteEngine On
    
    # get rewrite base dynamically
    RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)1$
    RewriteRule ^.*$ - [E=BASE:%2]
    
    RewriteRule ^sale/(.+)$ %{ENV:BASE}$1 [R=301,L,NC]
    

    Make sure these rules are first rules below RewriteEngine line.

  2. You could try using two capturing groups:

    RewriteRule ^(.*)sale/(.*)$ /$1$2 [NC,R=301,L]
    
  3. You need to put your code after RewriteEngine On

    So your code should be

    I can see n your .htaccess file below code

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /domain.com/
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /akasiayachting.com/index.php [L]
    </IfModule>
    

    Just put below code after RewriteBase /domain.com/ this line

    RewriteRule ^/sale/(.+)$ /$1 [R=301,L]
    

Comments are closed.