0

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.

MrWhite
  • 43,179
  • 8
  • 60
  • 84

1 Answers1

1

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

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • 1
    Yes, this /bet/r/index.php?r=fad5v is correct. It didn't work – Louis Web Solutions Jan 22 '20 at 21:29
  • What do you mean by "didn't work" exactly? Did you get an error? Incorrect rewrite? Nothing? What is the URL you are requesting? Do you have any other directives in your `.htaccess` file (although I guess not from your comment above)? Any other `.htaccess` files along the filesystem path? (I've updated my answer with respect to `index.php`.) – MrWhite Jan 22 '20 at 21:43
  • The error is "Object not found! The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again." I'm trying to access the URL from http://example.test/fad5v. I've tried the update answer, it's still giving the Object not found error. – Louis Web Solutions Jan 22 '20 at 22:10
  • You stated in the question that the URL was `/bet/fad5v`, not `/fad5v`? – MrWhite Jan 22 '20 at 22:12
  • You're right, but /bet/fad5v is giving the same error – Louis Web Solutions Jan 22 '20 at 22:19
  • Just try putting some `nonsense` at the start of the `.htaccess` file... do you get an error? (You would expect a 500 Internal Server Error). – MrWhite Jan 22 '20 at 22:21
  • Yes I got 500 error by adding some nonsense to the start of the htaccess fine. – Louis Web Solutions Jan 22 '20 at 22:33
  • Try temporarily changing the directive to read: `RewriteRule ^bet/([^/]+)$ /bet/r/index.php?r=$1 [R,L]` (note the additional slash at the start of the _substitution_ and the `R` flag) - do you get redirected? (and remove the _nonsense_.) – MrWhite Jan 22 '20 at 22:43
  • It still didn't work. Showing the object not found error – Louis Web Solutions Jan 22 '20 at 22:50
  • And there are no other directives in this `/.htaccess` file? Do you have any other `.htaccess` files at `/bet/.htaccess` or `/bet/r/.htaccess`? – MrWhite Jan 22 '20 at 22:58
  • I don't have any other directories in the htaccess file. I don't have it anywhere else. I'm actually testing from xampp and LoadModule rewrite_module modules/mod_rewrite.so is currently enebled – Louis Web Solutions Jan 23 '20 at 05:33
  • I've edited my answer to include a rule to help with debugging. Please try that and report back what you see. Thanks. – MrWhite Jan 23 '20 at 12:31
  • This worked. /bet/fad5v is now redirecting to /bet/r/index.php?r=fad5v as expected however, the main directory /bet/ is also redirecting to /bet/r/index.php?r=. This is affecting all other files in this directory. I want only the /r/index.php?r=fad5v rewritten to /bet/fad5v. – Louis Web Solutions Jan 23 '20 at 13:23
  • "This worked" - Although that wasn't necessarily meant as a solution - that was just to try and debug what is going on. The output you are seeing from that last directive implies this `.htaccess` file is located inside the `/bet` subdirectory, not in the document root as intended - is that the case? – MrWhite Jan 23 '20 at 15:25
  • Yes, /bet is the directory in htdocs. I'm using this in xampp. Am I supposed to put the htaccess file in htdocs directory? – Louis Web Solutions Jan 23 '20 at 15:33
  • "Am I supposed to put the htaccess file in htdocs directory?" - Yes. That's what I stated in my first sentence, "...your **root** `.htaccess` file"! ie. the `.htaccess` file in your _document root_. I would assume `htdocs` is your _document root_ directory? I also asked this in an [earlier comment](https://stackoverflow.com/questions/59866986/how-do-i-remove-querying-srting-from-my-url/59867940?noredirect=1#comment105869878_59867940) yesterday, but this appears to have been missed. – MrWhite Jan 23 '20 at 15:45
  • 1
    Is there any issue with having this in the root directory? To be honest, for this task it doesn't really matter which `.htaccess` file you use: `/.htaccess`, `/bet/.htaccess` or `/bet/r/.htaccess`. But you should only use one and, _importantly_, the directives you use will differ depending on which directory the `.htaccess` file is located. – MrWhite Jan 23 '20 at 15:47
  • I tried it in htdocs directory, it gave some redirection error when I visit /bet. I then put it in the main xampp directory, /bet is working but /bet/fad5v is no longer redirecting to /bet/r/index.php?r=fad5v. – Louis Web Solutions Jan 23 '20 at 15:54
  • 1
    Your updated answer worked perfectly. Thanks a million! – Louis Web Solutions Jan 23 '20 at 16:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206559/discussion-between-louis-web-solutions-and-mrwhite). – Louis Web Solutions Jan 24 '20 at 06:04