Closed (works as designed)
Project:
Accelerated Mobile Pages (AMP)
Version:
7.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
20 Mar 2017 at 17:39 UTC
Updated:
3 Jun 2023 at 10:10 UTC
Jump to comment: Most recent, Most recent file

Comments
Comment #2
marcelovaniI started the work here, it seems to be working fine, I even added some WebTests.
Comment #3
guardiola86 commentedThe patch works well. I think it's an option the module should offer.
I only had to make a small change in hook_uninstall replacing:
With:
Comment #4
marcelovaniSmall change, moved the URL options inside a fieldset
Comment #5
thepractice commentedI've added and removed some features from #4 in the interest of simplicity and utility.
I've added the option of using the amp url suffix (www.example.com/article/amp), since this seems to be a popular choice, alongside the amp prefix (www.example.com/amp/article) and amp subdomain (amp.example.com/article). For example:
www.newyorker.com/news/john-cassidy/donald-trumps-russian-marginalia-pro...
www.gq.com/story/russia-donald-trump-cnn-report/amp
nypost.com/2017/01/23/how-trump-is-remaking-the-culture-war/amp/
I've also removed the ability to choose the text of the url prefix, limiting the user to just the "www.example.com/amp/article" option. This seems simpler since the user doesn't have an option to choose the text of the query string (www.example.com/article?amp).
I've removed the added helper functions in the interest of simplicity.
I streamlined the logic is amp_is_amp_request() to more closely match the original.
And to convert from amp to non-amp paths for the prefix and suffix options, I've changed the simply cut out the prefix or suffix using substr(), instead of exploding the path on "/".
Comment #6
thepractice commentedWe still need to change the link rel="amphtml" tag for the new url formats. I will update #5 with this next.
Comment #7
marcelovaniAdding a new option to use as suffix is ok.
Fixing the value to 'amp' instead of allowing the user to change if they want, it debatable, it doesn't mean a lot of code.
About the code:
1 - Why are you removing the tests?
2 - Why are you removing the hook_uninstall?
I haven't tested the patch, does it work well with Context module for placing blocks on amp nodes?
Does it work with either url alias or node/nid?
Comment #8
thepractice commentedRegarding the comments in #7:
If we're going to allow the user to change amp prefix to arbitrary string, we should allow same for suffix and query string. I don't think it's needed at this point.
1. I added back in the tests, adding a test for suffix case and adjusting the other cases.
2. I added back in the hook_uninstall.
This new patch works with either url alias or node/nid.
For the Context module, for the new prefix and suffix modes, it does not appear to be possible to add a path rule for targeting amp pages. However, this appears to be the case for patch #4 as well (make_url_format_optional-2862218-4.patch).
Other:
I fixed so the link rel="amphtml" tag is correct for the new url formats.
I also fixed the way we set the system path in hook_url_inbound_alter().
Comment #9
marcelovaniI tested the patch by creating a node, setting the path alias to amp-test
Then I switched between the 3 url formats and visited the pages.
Some of the results are 404, I think we should always show a page, never 404.
Here are the test results, the cases that are 404ing need to be addressed:
Ps: We should add these checks to simpletest
Comment #10
marcelovaniJust ignore the table above, we probably should not worry about this, in case someone switches the configuration, google will crawl the pages again and use the link rel=”amphtml” to point to the correct page.
I have improved the tests, by checking that the correct link rel and canonical are displayed on full node display and amp display
The patch seems to be working ok
Comment #11
kalpaitch commentedOne little point found so far on this patch, seems like it will only alter inbound URLs if found in the alias tables by drupal_lookup_path()
Comment #12
kalpaitch commentedA couple of little suggestions:
Ideally all 'amp' paths would be registered as system routes to avoid collisions and avoid other modules defining those paths but not necessary now.
Comment #13
kalpaitch commentedComment #14
marcelovaniRe-rolling the patch.

One more thing that needs to be done:
The Save and view AMP buttons need to use the url format selected
Comment #15
marcelovaniJust got into a situation using patch #12 where amp_url_inbound_alter() is called from amp_html_head_alter() when trying to figure out the canonical url.
The url was being chopped from the beginning, so I added an extra check to only affect the urls that contain prefix/suffix.
Comment #16
thepractice commented[removed]
Comment #17
thepractice commented[removed]
Comment #18
thepractice commentedPlease ignore patches 16 and 17. I had not cloned the latest 7.x-1.x dev branch.
Patch 15 works for me, and passes all current tests.
Comment #19
kalpaitch commentedMoved the formatting of output urls to hook_outbound_url_alter to centralising the processing of amp url formats.
This allows redirection on submit to be handled with very little change.
Comment #20
thepractice commentedI ran the tests on patch 19, and it results in 4 failures. However when I check the failures manually, the behavior appears to be correct.
The 'failures' relate to finding the expected '
Comment #21
marcelovaniWe need to implement #14 and update the tests
Comment #22
guardiola86 commentedI've created a new patch with the changes in #14
Comment #23
guardiola86 commentedComment #26
kalpaitch commentedThe patch #19 did not need any changes, just re-rolling. Tests needed to be updated.
228 passes, 0 fails, 0 exceptions, and 67 debug messages in 2 min 15 sec.
However this is with amptheme installed, which the CI bot will not do because as far as I can tell there is no way to make the amptheme a dependency for the module.
That's why I'm attaching a second patch excluding the test steps that fail when the amptheme is not enabled.
Comment #27
kalpaitch commentedComment #30
kalpaitch commentedAll these fails are because the amptheme is not installed, we need to find a way to force the amptheme to be installed.
Comment #31
marcelovaniDisabling the tests that will not pass until we find a way to make drupal CI checkout amptheme
Comment #33
marcelovaniFixing tests
Using regex instead of $base_url
Comment #35
marcelovaniTrying with clean_urls
Comment #36
rainbowarrayJust wanted to note some history on this one.
There were a fair number of people in the Drupal community who strongly suggested using query strings rather than path aliases. Generating all of the additional aliases was particularly problematic on sites with a lot of nodes, if I remember correctly. Something like that.
Anyhow, here is a link to a past issue where this was discussed and some of the changes were made to move to query string switching. https://www.drupal.org/node/2676922
Giving people the option to use a path may work, it just may also have some drawbacks.
Comment #37
alimc29 commentedAre there any additional steps needed to get these redirects to work?
If I select prefix (or same behavior with suffix), example.com/amp/my-url seems to just be redirecting me to example.com/my-url
I am looking into this option because I am encountering some odd behaviors with caching. It seems I can consistently trigger an issue with caching on my dev site when I do the following:
So, as a work-around, I was looking at this patch to see if it might resolve my caching issue, but I can't get the URL to not redirect to the non-amp version
Has anyone had any luck with this or have any advice?
thanks!
Comment #38
erku commentedIs it possible to make this part of the module? It's really important to have what we call "clean URLs." I notice that on Google top stories those sites rank more often that have clean urls, like /amp instead of ?amp
Thank you.
Comment #39
erku commentedDoes anyone have any solutions to #37?
I am having the same issues.
Thank you for help.
Comment #40
marcelovani@mdrummond This patch doesn't use redirects.
All incoming requests are handled on the fly by hook_url_inbound_alter() and the output of links handled by hook_url_outbound_alter().
I think the only problem we could encounter is when the node URL changes after it's cached by google.
Since we don't deal with amp redirects, a person coming from a cached AMP page with the previous URL could hit the backend and be served with a 404 page.
The solution for that is simply clear the AMP cache for that page, which is supported by google AMP.
When you clear the cache, the amphtml link will point to the new URL.
@alimc29, @erku what patch are you using? As you can see on patch #35, all tests are are passing. Did you encounter any edge case that is not being covered by the tests?
With regards to the proposal of supporting different ways of building the urls, it's of course debatable and I can see good and bad points from both sides.
As long as the 3 options do work, I think it's a nice thing that users can actually choose what they want to use.
We are running patch #35 on the following sites for 3 months and haven't seen any issues.
These are high traffic sites and if something was wrong, we would hear from our SEO team.
http://www.theweek.co.uk/amp/node/77060
http://www.knowyourmobile.com/amp/mobile-phones/samsung-galaxy-a5/24297/...
http://www.carbuyer.co.uk/amp/news/156748/volkswagen-id-crozz-joins-comp...
Comment #41
erku commentedThank you Marcelo,
I tried it, but I am geting this.
mysite.com/url looks good
mysite.com/url/amp (or other other aliased version) redirects to mysite.com/url
What to do about this? What is causing the URL? I am using https if that matters.
Thank you
Comment #42
erku commentedHello everyone. If the patch doesn't work that means you are using Global Redirect, which redirects it to the original URL. Thank you @marcelovani.
Now the question is, is there a way to make the patch work with Global Redirect? Global Redirect is an important module and running in many sites making vital function. Does anyone have a solution on how to make the patch ignore global redirect?
Thank you.
Comment #43
alimc29 commented@erku (& @marcelovani) - Thanks for tracking that down to the Global Redirect module!
It looks like one point of Global Redirect is to avoid having 2 URLs with the same content. However, with AMP, we actually do want this - mostly the same content available via 2 unique URLs; one is just specific to AMP.
In response to @erku's question about how to get around this issue with Global Redirect, I haven't had time to work on this or anything, but looking at the Global Redirect's hook_init function, it looks like it uses hook_url_outbound_alter twice; per the comments on its implementation of hook_url_outbound_alter:
I'm wondering if potentially we could look more into hook_url_outbound_alter - including looking at how locale (in core) handles it for an example?
Comment #44
marcelovaniI don't see the problem with duplicated content.
If the canonical url is set correctly and it is the same, no matter which url it resolves, google will be happy about it.
i.e.
Using a custom url
url: http://www.example.com/my-test-url
canonical should be: http://www.example.com/my-test-url
Using node/nid
url: http://www.example.com/node/1
canonical should be: http://www.example.com/my-test-url
Using query string
url: http://www.example.com/?q=node/1
canonical should be: http://www.example.com/my-test-url
AMP page using query string
url: http://www.example.com/node/1?amp
canonical should be: http://www.example.com/my-test-url
AMP page with prefix
url: http://www.example.com/amp./my-test-url
canonical should be: http://www.example.com/my-test-url
Do you really need Global Redirect module?
Comment #45
davidgrayston commentedWe have encountered an issue with patch #35 whereby the we are calling
amp_is_amp_request()too early in the bootstrap, and as a result after a cache clear,amptheme_html_head_alter()isn't invoked, which means some html head elements aren't being removed.amptheme_html_head_alter()is invoked on subsequent page requests, but it's possible (and likely) the page would have already been cached with the invalid head elements.This problem only occurs if there are hooks invoked early in the bootstrap that lead to the theme being initialised. In our situation an entity load occured in
redirect_url_inbound_alter(), which in turn caused a contrib module to call thel()function in its implementation ofhook_filter_info().We looked into refactoring the patch so that
amp_url_inbound_alter()only checks the requested path to determine if an amp page is being requested, as this prevents any risk of entities being loaded too early, but we feel this could lead to other unforeseen issues, such as resolving paths it shouldn't.After some consideration and discussion with our SEO team and @marcelovani, we decided to phase out the use of this patch and use the
?ampquery string instead for the following reasons:- If a node URL changes, the redirect module can resolve the
?amppath- Altering the path
amp_url_inbound_alter()carries some risk of unpredictable behaviour depending on which modules sites have enabled (as we are currently experiencing)Comment #46
marcelovaniAgreed with #45 and #36. I did not foresee all these issues with urls.
After speaking to the SEO team, they confirmed that it should make no difference to Google rankings if we use prefix/suffix or query string.
I am going to close this issue.
If anyone strongly believe there is any advantage in using this patch, feel free to continue to use it.
Comment #47
erku commentedHello, is there a way to work /amp URLs in Drupal 9? We are trying to migrate our website from D7 to D9, and would like to know how to make our AMP URLs ending with /amp rather than with a question mark. Can you please help how to do this in Drupal 9?
Thank you.