.htaccessurlmod-rewriteurl-rewritingpretty-urls

How do I rewrite my URL to the underlying filesystem path with a query string?


I need help with URL rewrite. I will like to make this URL (https://example.test/bet/r/?r=fad5v) look like this (https://example.test/bet/fad5v).

What's the best way to do this so that when https://example.test/bet/fad5v is visited, it opens the actual URL https://example.test/bet/r/?r=fad5v.

Any help will be appreciated.


Solution

  • Try the following near the top of your root .htaccess file:

    RewriteEngine On
    
    RewriteRule ^bet/([^/.]+)$ bet/r/index.php?r=$1 [L]
    

    UPDATE: From comments, it seems you have been putting this directive in the .htaccess in the /bet subdirectory (ie. /bet/.htaccess), not the document root directory as mentioned above.

    If you are using the /bet/.htaccess file then you will need to modify the above directive(s) to read:

    RewriteEngine On
    
    RewriteRule ^([^/.]+)$ r/index.php?r=$1 [L]
    

    It is advisable to make the regex as restrictive as possible so as not to conflict with other resources (eg. can this code only consist of lowercase a-z and digits?). You'll note that I've added the dot to the character class to avoid conflicting with actual files (as you noted in comments).