5

I am re-writing an old project using Symfony2 so I can try out the framework. I have urls that will be of the form:

/profile/{id}

in the snazzy way that Symfony2 does it. However, the same page was originally found by doing:

/profile.php?id=12345

So, in case someone has an old URL, I'd like to redirect these links. The problem is, I don't know how to catch routes of this nature. I tried

/profile.php?id={id}

but that didn't seem to work. How can I set up this route?

Follow-up: I do not want to do "catch-all" (because it is non-intuitive to me so I fear future bugs), and I would prefer not to do it via htaccess for the same reason. I think the best option is to match "/profile.php" then in the controller, check that "id" exists in query-string and redirect accordingly. If it does not, I will redirect to 404.

1 Answer 1

3

I see two options here:

  1. You map your old schema (/profile.php?id=54321) onto the new (/profile/54321) using mod_rewrite (in case you use Apache).

  2. You write a mapper within Symfony. That means at the end of your list of routes you specify a pattern that will just catch everything not yet catched:


whatever:
  pattern: /{whatever}
  defaults: { _controller: CoreBundle:Default:whatever }
  requirements:
    whatever: .+

For (2) you will have to check what the Request-object offers you for the queries (like getQueryString()), b/c I am not sure if it is possible to have something like ?xyz being matched in a route.

Sign up to request clarification or add additional context in comments.

7 Comments

Would the correct mod_rewrite be: RewriteRule ^profile.php?id=(\d+)$ /profile/$1 [R=301] ?
I just want to verify this is the best strategy, since I'm not an expert in Apache htaccess shenanigans
Me neither. You can test rewrites here: martinmelin.se/rewrite-rule-tester. Though I would prefer to handle that with SF. Because routes are nowadays part of the model/logic.
@raffael1984 post params have nothing to do with routes. He should check the params in his controller, e.g $request->query->getInt('id'). This always returns an integer.
neither me nor martin is talking about POST-parameters
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.