0

I want to remove index.php from my URL after Query Params.

This is My Url:

http://127.0.0.1/user/report?user=USERNAME

I have removed Query Params and convert it into Pretty Url using:

    RewriteCond %{QUERY_STRING} !user=
    RewriteRule ^([a-zA-Z0-9\-]+)/(.*)$ $2?user=$1 [L,QSA]
    RewriteRule ^([a-zA-Z0-9\-]+)$ ?user=$1 [L,QSA]

Now, My URL looks like this:

http://127.0.0.1/user/report/USERNAME

So all the requests to this URL will point to the entry script of my project i.e. web/index.php.

When I use below routes to get data, it works:

http://127.0.0.1/user/report/Default/index.php/api/registration/user-registrations/

But when I remove index.php from URL and access it like below, it throws 404.

http://127.0.0.1/user/report/Default/api/registration/user-registrations/

FYI, I am using Symfony for routing all my routes.

I am stuck in this from the past 2 weeks. Please help.

1 Answer 1

0

The problem is that you have no rule to rewrite the URL without index.php into a URL with!

As you say, the URL which works (#1) is:

http://127.0.0.1/user/report/Default/index.php/api/registration/user-registrations/

And the URL you want to get to work (#2) is:

http://127.0.0.1/user/report/Default/api/registration/user-registrations/

The goal of a rewrite is to help the server understand what a URL means so that it can serve it correctly. So, you need a rule to rewrite #2 into #1.

Something like this:

RewriteRule ^(/user/report/Default)/(.*)$ $1/index.php/$2 [L,QSA]

Please note that I haven't tested this so you may need to experiment a little. It's possible that you might need to remove the 'L' flag.

Edit

Here's a more full example, showing how you can use RewriteRules to build up to what you want, as they run in sequence.

I've modified your rewrite rule - rather than using a-zA-Z0-9-, which would not match index.php, i just match the last part of the URL. Regular expression syntax is tricky and worth getting to grips with, but I appreciate it's a difficult topic.

    RewriteCond %{QUERY_STRING} !user= 
    RewriteRule ^(.*)/([^/]*)/?$ /index.php$1?user=$2 [QSA,L]

This sort of thing should work.

(for anyone coming to this reply fresh, i iterated on the rules directly with the question asker in order to clarify the setup.)

9
  • The problem is USERNAME is dynamic so I can't directly rewrite rule like you said ^(/user/report/Default)/(.*)$. May 17, 2020 at 14:37
  • It's no problem at all. RewriteRules run in sequence. I'll add a more full example to my answer.
    – Ben XO
    May 17, 2020 at 14:39
  • Here Username is Default. So it will be different every time. Like http://127.0.0.1/user/report/Akshay and http://127.0.0.1/user/report/Ben. May 17, 2020 at 14:48
  • i had to take a guess at the relationship between "user/report/Default/api/registration" and "user/report/USERNAME". I assumed that /user/report/Default was just where the files were uploaded to, and all of the rewriting was on parts after that. If that's not the case, please can you clarify the before-and-after that you need.
    – Ben XO
    May 17, 2020 at 14:48
  • okay - and where do you need index.php to go? AFTER the username?
    – Ben XO
    May 17, 2020 at 14:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .