Anything after the #
is a fragment, and will not be sent to the webserver. You cannot capture it at any point there, you'll have to use a client-sided approach to capture those.
Hi All,
I know this is a question more suited for other forums but just wanted to get an opinion if anyone has a handy rewrite rule for the below use case -
I want to redirect a URL for the format "https://abc.com/x/y#z" to "https://abc.com/"
The typical rewrite rule which we use is as below -
RewriteRule ^/x/y / [R=301,L,NE]
This however yields the URL - https://abc.com/#z
Changing the rule to below (including # in Rewrite rule) leads to the rule not being met at all -
RewriteRule ^/x/y#z / [R=301,L,NE]
Does anyone have a handy regex rule for handling the above use case?
@Imran__Khan, @arunpatidar, @SureshDhulipudi, @Raja_Reddy ,@aanchal-sikka, @EstebanBustamante
Thanks in advance,
Rohan Garg
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
yes, you need to create a config in AEM and expose as json and based on entry, perform client side redirect.
"the anchor tag is never sent as part of the HTTP request by any browser. It is only interpreted locally within the browser".
The new rule would be:
RewriteRule ^/x/y$ / [R=301,L,NE]
Thanks for the prompt reply @arunpatidar! Currently we are looking at the URLs which might be bookmarked which often includes the anchored part as well.
However, if you run the above redirect on the htaccess tester you will get the below result (https://htaccess.madewithlove.com?share=0e704428-323e-461d-9cfe-34bc3b5f6df6)
The URL bounces in endless redirection from https://www.abc.com/#z to https://www.abc.com/%23z
I believe what Arun meant is that there should not be a scenario like the one you are describing. Even if the URL contains the anchor tag, it shouldn't reach Apache. Therefore, the request to Apache would be without the anchor tag. Hence, the rule that @arunpatidar shared should work just fine.
Hope this helps.
Hey @EstebanBustamante , thanks! I had just caught up on that comment by @arunpatidar now!
As per Handle Anchor using mod-rewrite too, here's the answer -
Anything after the #
is a fragment, and will not be sent to the webserver. You cannot capture it at any point there, you'll have to use a client-sided approach to capture those.
Views
Replies
Total Likes
Hi @Rohan_Garg
You are doing rewrite, not the redirect, if you do redirect # will be gone from the url.
Views
Replies
Total Likes
Hi @Rohan_Garg
If the source and target urls are the same.
Then you need to handle this at clienside, where you redirects user from https://www.abc.com/#z to https://www.abc.com/
Just FYI : there is a space in your test rule.
Here is the link, which I tested.
Hey @arunpatidar, sorry for using redirect and rewrite interchangeably!
I understand that the /#randomString to / has to happen at browser and not webserver.
But the problem is the page in question has lot of valid sections as well - so we don't want to simply strip off #randomString - The only way I see is we fetch all sections in the page and then check if the rewrite hash is valid or not.
Let me know if this approach and understanding is incorrect!
Views
Replies
Total Likes
yes, you need to create a config in AEM and expose as json and based on entry, perform client side redirect.
"https://abc.com/x/y#z" to "https://abc.com/" using Apache2 for AEM as a Cloud Service (AEMaaCS), it's important to understand that URL fragments (the part of the URL following the #
symbol) are handled client-side by the browser and are not sent to the server. This means that the Apache server does not receive the fragment part of the URL and cannot directly perform a redirect based on it.
If you are planning to redirect #hash tags, then you must put together a client-side solution in Javascript, for example:
if (window.location.hash === '#z') {
window.location.href = 'https://abc.com/';
}
Hi @BrianKasingli, Thanks for the reply!
Views
Replies
Total Likes
@arunpatidar - Thanks a lot for your support as always!
Views
Replies
Total Likes
@Rohan_Garg Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.
Hey @kautuk_sahni, I will be sure to mark this query resolved once done! Thanks!
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies