regex.htaccesshttp-redirectmod-rewritedocument-root

How to rewrite for existing rewrite path?


I have this code in my .htaccess file that redirects users who go to www.example.com/inventory/anySKUnumber:

RewriteEngine On

# Change URL to .../inventory/$row[number]
RewriteRule ^Inventory/([^/]*)$ /Inventory/vendors/php/LandingPage/DirectSKU.php?number=$1 [QSA,L,NC]

Now I also want, if the user goes to www.example.com/inventory they get redirected to the homepage but I want to keep the same path in the address bar [not just redirect to it].

I get redirected but with an empty path in the address bar when trying this:

RewriteEngine On

# Change URL to .../inventory/$row[number]
RewriteRule ^Inventory/([^/]*)$ /Inventory/vendors/php/LandingPage/DirectSKU.php?number=$1 [QSA,L,NC]

# Redirect to index but keep path
RewriteRule ^Inventory/?$ /$ [QSA,NC]

How do I redirect while keeping the path (http://www.example.com/inventory)?


Solution

  • In the first rewrite rule there is an asterisk used. An asterisk can match zero characters, which is unwanted. Using a plus instead solves the issue. Here is the full code:

    RewriteEngine On
    RewriteBase /
    
    # Change URL to .../Inventory/$row[number]
    RewriteRule ^Inventory/([^/]+)$ /vendors/pages/Inventory/LandingPage/DirectSKU.php?number=$1 [QSA,L,NC]
    
    # Redirect to index but keep paths
    RewriteRule ^Inventory/?$ index.php [QSA,NC]