Search code examples
regexapache.htaccessmod-rewrite

htaccess short url with emoving forward slash


my .htaccess:

RewriteEngine on
RewriteRule ^(.*)$ ../menu.php?m=$1 [R=301,NC,L]

when someone goes to example.com/folder/text will redirected to examle.com/menu.php?m=text that's work fine,

but my problem is when someone write a forward slash or another folder

Ex: example.com/i/text/ it's redirected to examle.com/menu.php?m=text/ NOT to examle.com/menu.php?m=text

so, my question can I remove all text after forward slash even if it's a file or folder

Ex: example.com/i/folder/ OR example.com/i/folder/ssw/text.html will redirected to examle.com/menu.php?m=folder


Solution

  • Remove $ and use a negate character class like [^/]+:

    RewriteEngine on
    
    RewriteRule ^([^/]+) ../menu.php?m=$1 [R=301,NC,L,QSA]
    

    Make sure to clear your browser cache completely before testing this change. This assumes that this .htaccess is inside a sub-directory and menu.php is in site root.