modrewrite website with slash in .htaccess

785 Views Asked by At

How can I translate an URL like: http://localhost/mySite/?link=OFFERS&sublink=ARTICLE&subsublink=DETAIL

to: http://localhost/mySite/OFFERS/ARTICLE/DETAIL

if one parameter is empty it should still work like this: localhost/mySite/?link=OFFERS&sublink=ARTICLE localhost/mySite/OFFERS/ARTICLE

Extra problem: There is an enter page under index.php and the rewrite should work with index2.php. Best would be if it would work under localhost and on live system without changes.

Currently I'm using: RewriteRule ^([^/.]+)$ index2.php?link=$1 [L] But that only works for one parameter and I couldn't improve this code for ages ^^

2

There are 2 best solutions below

0
On BEST ANSWER
RewriteRule ^([^/.]+)(/([^/.]+))?(/([^/.]+))?$ index2.php?link=$1&sublink=$3&subsublink=$5 [L,QSA]

Note that localhost/mySite/OFFERS/ARTICLE links to localhost/mySite/?link=OFFERS&sublink=ARTICLE&sussublink= and not localhost/mySite/?link=OFFERS&sublink=ARTICLE. Should not be a big issue, but make sure the PHP code doesn't us isset($_GET['subsublink']).

3
On

Try adding the following to your htaccess file in the mysitedirectory of your site.

RewriteEngine on
RewriteBase /mysite/

# rewrite http://localhost/mySite/OFFERS/ARTICLE/DETAIL to
# http://localhost/mySite/?link=OFFERS&sublink=ARTICLE&subsublink=DETAIL
#assumes that index2.php is in the root directory of site
RewriteRule ^([-a-zA-Z0-9])+/([-a-zA-Z0-9])+/([-a-zA-Z0-9])+ index2.php?link=$1&sublink=$2&subsublink=$3 [NC,L]

#redirect index to index2
#if you do not want to redirect, just server rewrite, remove the R=301 below
RewriteRule ^index\.php$ index2.php [NC,L,R=301]