Skip to content

Fix URL rewrite match search when switching store #20093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

aapokiiso
Copy link
Contributor

@aapokiiso aapokiiso commented Jan 6, 2019

Description (*)

When "Use Store Code in URLs" is enabled, Magento 2 currently redirects to the homepage if changing the store from a product, category or CMS page. So basically any page which uses URL rewrites. There has been some work done on this by Magento (see issue #2786), but there's no definitive solution yet. This PR aims to fix the issues with the following changes:

  • Uses target path to match URL rewrites between
    stores. Request path (used previously) can be
    different between stores.
  • Changes the target URL if matching
    URL rewrite is found. Previously it just
    redirected to the homepage in case the rewrite
    wasn't found, and did nothing if found.

Fixed Issues (if relevant)

  1. Store View Switching #2786: Store View Switching

Manual testing scenarios (*)

  1. Start with a clean Magento installation.
  2. Create a new store view called "Test store" with the code "test" in the "Main Website" website.
  3. Enable "Use Store Code in URLs" globally.
  4. Create a product with a URL key "product-1" in the "Default Store View" scope.
  5. Change the scope to "Test store" in the product editor.
  6. Set the product URL key as "product-1-test-store" in the "Test store" scope. Save the product.
  7. Navigate to the frontend http://<your-store.com>/default/product-1 (eg. in the default store)
  8. Change the store through the store switcher to "Test store". This should go through the stores/store/redirect controller. Magento will redirect you http://<your-store.com>/test, aka the homepage for "Test store". This PR will change it so that the user remains on the product page, eg. http://<your-store.com>/test/product-1-test-store.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

Notable changes:
- Uses target path to match URL rewrites between
  stores. Request path (used previously) can be
  different between stores.
- Changes the target URL if matching
  URL rewrite is found. Previously it just
  redirected to the homepage in case the rewrite
  wasn't found, and did nothing if found.
@magento-cicd2
Copy link
Contributor

magento-cicd2 commented Jan 6, 2019

CLA assistant check
All committers have signed the CLA.

@magento-engcom-team
Copy link
Contributor

Hi @aapokiiso. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento-engcom-team give me test instance - deploy test instance based on PR changes
  • @magento-engcom-team give me 2.3-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Assistant documentation

@davidverholen
Copy link
Member

@magento-engcom-team give me test instance

@magento-engcom-team
Copy link
Contributor

Hi @davidverholen. Thank you for your request. I'm working on Magento instance for you

@davidverholen davidverholen self-requested a review January 6, 2019 11:33
@davidverholen davidverholen added the Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release label Jan 6, 2019
@magento-engcom-team magento-engcom-team added this to the Release: 2.3.1 milestone Jan 6, 2019
@davidverholen
Copy link
Member

davidverholen commented Jan 6, 2019

the test seems to fail since url rewriting (index.php to /) is not enabled in the integration test suite.
This leads to the url being generated as http://localhost/index.php/page-c/ (which is correct in this case) instead of the (currently) expected http://localhost/page-c

@aapokiiso can you update the respective test case and see if the build succeeds please

aapokiiso and others added 3 commits January 6, 2019 14:18
"/index.php" -> "/" rewrite is not configured
on test servers, so we need to include "index.php"
in test URLs.
@magento-engcom-team
Copy link
Contributor

@aapokiiso thank you for contributing. Please accept Community Contributors team invitation here to gain extended permissions for this repository.

@hostep
Copy link
Contributor

hostep commented Jan 6, 2019

Thanks @aapokiiso
But be aware that there is another PR which attempts to solve the same issue: #19798

Not sure which one of these two is the better one...

@aapokiiso
Copy link
Contributor Author

@hostep Nice find, they seem to achieve the same thing with some minor differences.

My PR selects the first matching URL rewrite, whereas #19798 selects the last one. As I understand, normally there wouldn't be multiple URL rewrites for the same target path + store combination, so this shouldn't matter.

The other difference is the target URL generation based on the rewrite request path. My PR uses Magento\Store\Model\Store::getUrl, whereas #19798 combines the store base URL with the request path using PHP string concatenation. Again, this shouldn't make any difference in real life.

@davidverholen
Copy link
Member

@aapokiiso the test is failing again, now having http://localhost/index.php/page-c-on-2nd-store/ as the redirected url.
It seems correct if the fixture has this url set on the second store. Not sure why it was still another url before

Magento\Store\Model\Store::getUrl expects the $route
parameter to be a module/controller/action combination,
not a free-form URL path like in this case.
…pokiiso/magento2 into url-rewrite-switch-store-correct-path
@aapokiiso
Copy link
Contributor Author

@davidverholen As I understand, the code is performing correctly but the test needs to be fixed to expect http://localhost/index.php/page-c-on-2nd-store, since that's what the page URL is in the second store (see dev/tests/integration/testsuite/Magento/UrlRewrite/_files/url_rewrite.php:115). Or am I misunderstanding the purpose of this test?

@davidverholen
Copy link
Member

@aapokiiso I think you are right and the test was basically wrong succeeding before since it should have expected the url key form the second store

@okorshenko okorshenko removed this from the Release: 2.3.1 milestone Jan 28, 2019
@davidverholen
Copy link
Member

Hi @aapokiiso,
thank you for your contribution, I'm closing this as a duplicate of #19798 as was stated before.

@ghost
Copy link

ghost commented Jan 30, 2019

Hi @aapokiiso, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

@aapokiiso
Copy link
Contributor Author

@davidverholen Ok, thanks for the info! By the way, the "Contribution Survey" link above is broken.

@davidverholen
Copy link
Member

@aapokiiso thanks for the hint, I'll try find someone where I can forward this to ;)

@davidverholen
Copy link
Member

@aapokiiso did you login on the page via github? Just wondering if it is maybe only available for the PR author

@sidolov
Copy link
Contributor

sidolov commented Jan 30, 2019

Hi @aapokiiso , @davidverholen survey link was fixed, you may try once again. I'm sorry for the inconvenience

@hostep
Copy link
Contributor

hostep commented Jan 30, 2019

@sidolov: sorry to intrude in this thread, I also recently got such a link in an accepted PR, and now it works indeed. But it asks me to login and then says I have to give Magento read and write access to all my public repo's. I'm not really comfortable with this, is there somehow a way to reduce the amount of permissions we must give you to login to https://opensource.magento.com/ ?

Exact permissions it asks are:

  • Magento Community Portal by magento wants to access your {username} account
  • This application will be able to read your private email addresses.
  • This application will be able to read and write all public repository data. This includes the following:
    • Code
    • Issues
    • Pull requests
    • Wikis
    • Settings
    • Webhooks and services
    • Deploy keys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants