While some people like to use query string ?amp to show AMP pages, others will agree that using a Url prefix is also a good idea i.e. /amp/foo-page

I would like to propose we introduce an option so that the site builders can decide if they want to use query string or url prefix.
The option defaults to query string, which is the current behaviour.

If they choose Path prefix, they can configure the prefix to anything else, if they don't want to use /amp

CommentFileSizeAuthor
#35 make_url_format-2862218-35.patch19.55 KBmarcelovani
#35 interdiff-35-33.txt543 bytesmarcelovani
#33 interdiff-33-31.txt6.34 KBmarcelovani
#33 make_url_format-2862218-33.patch19.48 KBmarcelovani
#31 make_url_format-2862218-30.patch18.7 KBmarcelovani
#31 interdiff-30-26.txt1.96 KBmarcelovani
#26 make_url_format-2862218-26-make-tests-pass.patch18.96 KBkalpaitch
#26 interdiff-2862218-15-26.do-not-test.patch23.02 KBkalpaitch
#26 make_url_format-2862218-26.patch18.35 KBkalpaitch
#22 make_url_format-2862218-22.patch14.46 KBguardiola86
#19 make_url_format-2862218-19.patch17.98 KBkalpaitch
#17 make_url_format-2862218-17.patch16.22 KBthepractice
#16 make_url_format-2862218-16.patch16.67 KBthepractice
#15 interdiff.txt883 bytesmarcelovani
#15 make_url_format-2862218-15.patch15.48 KBmarcelovani
#14 Screen Shot 2017-04-24 at 10.54.33.png16.85 KBmarcelovani
#14 make_url_format-2862218-14.patch15.27 KBmarcelovani
#12 interdiff-10-12.do-not-test.patch1.01 KBkalpaitch
#12 make_url_format-2862218-12.patch15.23 KBkalpaitch
#10 changes.txt6.07 KBmarcelovani
#10 make_url_format-2862218-10.patch14.96 KBmarcelovani
#8 amp-make_url_format_optional-2862218-8.patch12.63 KBthepractice
#8 interdiff-2862218-5-8.txt9.77 KBthepractice
#8 interdiff-2862218-4-8.txt12.04 KBthepractice
#5 interdiff-2862218-4-5.txt13.34 KBthepractice
#5 amp-make_url_format_optional-2862218-5.patch3.29 KBthepractice
#4 make_url_format_optional-2862218-4.patch12.08 KBmarcelovani
#3 make_url_format_optional-2862218-2.patch11.97 KBguardiola86
#2 make_url_format_optional-2862218-1.patch11.96 KBmarcelovani
Screen Shot 2017-03-20 at 17.17.43.png29.42 KBmarcelovani

Comments

marcelovani created an issue. See original summary.

marcelovani’s picture

Title: Make it optional to use query string or url prefix for AMP pages » Make Url format optional to use query string or url prefix for AMP pages
StatusFileSize
new11.96 KB

I started the work here, it seems to be working fine, I even added some WebTests.

guardiola86’s picture

StatusFileSize
new11.97 KB

The 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:

variable_del('amp_url_prefix');

With:

variable_del('amp_path_prefix');
marcelovani’s picture

StatusFileSize
new12.08 KB

Small change, moved the URL options inside a fieldset

thepractice’s picture

Assigned: marcelovani » Unassigned
StatusFileSize
new3.29 KB
new13.34 KB

I'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 "/".

thepractice’s picture

Assigned: Unassigned » thepractice
Status: Needs review » Needs work

We still need to change the link rel="amphtml" tag for the new url formats. I will update #5 with this next.

marcelovani’s picture

Adding 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?

thepractice’s picture

Assigned: thepractice » Unassigned
Status: Needs work » Needs review
StatusFileSize
new12.04 KB
new9.77 KB
new12.63 KB

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

marcelovani’s picture

I 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:

Url Format: Query string		
Path 			Expected Behaviour	Result
/node/1?amp		Show amp page		ok
/amp-test?amp		Show amp page		ok
/amp/node/1		Show normal page	404
/amp/amp-test		Show normal page	404
/node/1/amp		Show normal page	ok
/node-test/amp		Show normal page	404
		
Url Format: Prefix		
Path 			Expected Behaviour	Current Behaviour
/node/1?amp		Show normal page	ok
/amp-test?amp		Show normal page	ok
/amp/node/1		Show amp page		404
/amp/amp-test		Show amp page		ok
/node/1/amp		Show normal page	ok
/node-test/amp		Show normal page	404
		
Url Format: Suffix		
Path 			Expected Behaviour	Current Behaviour
/node/1?amp		Show normal page	ok
/amp-test?amp		Show normal page	ok
/amp/node/1		Show normal page	404
/amp/amp-test		Show normal page	404
/node/1/amp		Show amp page		ok
/node-test/amp		Show amp page		404

Ps: We should add these checks to simpletest

marcelovani’s picture

Title: Make Url format optional to use query string or url prefix for AMP pages » Make Url format optional to use query string/prefix/suffix for AMP pages
StatusFileSize
new14.96 KB
new6.07 KB

Just 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

kalpaitch’s picture

One 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()

+++ b/amp.module
@@ -327,6 +327,35 @@ function amp_entity_info_alter(&$entity_info) {
+  else {
+    $path = $non_amp_path;
+  }
kalpaitch’s picture

StatusFileSize
new15.23 KB
new1.01 KB

A couple of little suggestions:

  1. As per #11 previous patch only detects aliases, added backups to make internal system paths recognised.
  2. Added checks to ignore urls that are already valid routes.

Ideally all 'amp' paths would be registered as system routes to avoid collisions and avoid other modules defining those paths but not necessary now.

kalpaitch’s picture

marcelovani’s picture

Status: Needs review » Needs work
StatusFileSize
new15.27 KB
new16.85 KB

Re-rolling the patch.
One more thing that needs to be done:
The Save and view AMP buttons need to use the url format selected

marcelovani’s picture

StatusFileSize
new15.48 KB
new883 bytes

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

thepractice’s picture

StatusFileSize
new16.67 KB

[removed]

thepractice’s picture

StatusFileSize
new16.22 KB

[removed]

thepractice’s picture

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

kalpaitch’s picture

StatusFileSize
new17.98 KB

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

thepractice’s picture

I 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 '

marcelovani’s picture

We need to implement #14 and update the tests

guardiola86’s picture

StatusFileSize
new14.46 KB

I've created a new patch with the changes in #14

guardiola86’s picture

Status: Needs work » Needs review

The last submitted patch, 19: make_url_format-2862218-19.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 22: make_url_format-2862218-22.patch, failed testing.

kalpaitch’s picture

StatusFileSize
new18.35 KB
new23.02 KB
new18.96 KB

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

kalpaitch’s picture

Status: Needs work » Needs review

The last submitted patch, 26: make_url_format-2862218-26.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 26: make_url_format-2862218-26-make-tests-pass.patch, failed testing.

kalpaitch’s picture

All these fails are because the amptheme is not installed, we need to find a way to force the amptheme to be installed.

marcelovani’s picture

Status: Needs work » Needs review
StatusFileSize
new1.96 KB
new18.7 KB

Disabling the tests that will not pass until we find a way to make drupal CI checkout amptheme

Status: Needs review » Needs work

The last submitted patch, 31: make_url_format-2862218-30.patch, failed testing.

marcelovani’s picture

Status: Needs work » Needs review
StatusFileSize
new19.48 KB
new6.34 KB

Fixing tests
Using regex instead of $base_url

Status: Needs review » Needs work

The last submitted patch, 33: make_url_format-2862218-33.patch, failed testing.

marcelovani’s picture

Status: Needs work » Needs review
StatusFileSize
new543 bytes
new19.55 KB

Trying with clean_urls

rainbowarray’s picture

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

alimc29’s picture

Are 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:

  1. in mobile, view example.com/my-url?amp - good
  2. in mobile, view example.com/my-url - good
  3. in mobile view, view #1 again - still good
  4. clear drupal cache
  5. view #1 again - mistakenly renders as non-amp view (#2)

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!

erku’s picture

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

erku’s picture

Issue tags: +Looking for solution

Does anyone have any solutions to #37?

I am having the same issues.

Thank you for help.

marcelovani’s picture

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

erku’s picture

Thank 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

erku’s picture

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

alimc29’s picture

@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:

  // Allow Drupal to rewrite the URL
  // Most commonly this will be used to invoke locale_language_url_rewrite_url().
  // Via the locale_url_outbound_alter() implementation of hook_url_outbound_alter().
  // NOTE: We use $request_path here as current_path() is $_GET['q'] which gets
  //       altered by core, whereas $request_path is untouched
  drupal_alter('url_outbound', $request_path, $options, $request_path);

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?

marcelovani’s picture

I 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?

davidgrayston’s picture

We 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 the l() function in its implementation of hook_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 ?amp query string instead for the following reasons:
- If a node URL changes, the redirect module can resolve the ?amp path
- 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)

marcelovani’s picture

Status: Needs review » Closed (works as designed)

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

erku’s picture

Hello, 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.