简体   繁体   中英

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]

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 ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM