Search code examples
phpapache.htaccessmod-rewriteurl-rewriting

How to redirect url with forward slash in .htaccess?


I'm trying to redirect my url:

www.site.com/dashboard/invest-package.php?packageID=1

to

www.site.com/dashboard/invest-package/1

Actually I solved this problem with -

<a href="invest-package-'.$row['packageID'].'">Invest</a>

RewriteRule ^invest-package-(.*)$ invest-package.php?packageID=$1 [QSA,L]

But I wanted to make with "/" don't like using "-". Solutions I found on the internet didn't work. I keep getting 404 not found error.

Here is my link to invest-package.php

<a href="invest-package/'.$row['packageID'].'">Invest</a>

and .htaccess file:

Options -Indexes
RewriteEngine On

RewriteBase /dashboard/

RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]

RewriteRule ^invest-package/(.*)$ invest-package.php?packageID=$1 [QSA,L]

Solution

  • You may try this code:

    Options -Indexes -MultiViews
    RewriteEngine On
    RewriteBase /dashboard/
    
    RewriteCond %{REQUEST_FILENAME} !-d [NC]
    RewriteCond %{REQUEST_FILENAME} !-f [NC]
    RewriteRule ^invest-package/([\w-]+)/?$ invest-package.php?packageID=$1 [QSA,L,NC]
    

    It is important to turn off MultiViews i.e. the content negotiation service of Apache.

    Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.php without any GET parameters.