Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Apache Rewrite rules for handling URLs containing "#"

Avatar

Community Advisor

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

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

yes, you need to create a config in AEM and expose as json and based on entry, perform client side redirect.



Arun Patidar

View solution in original post

13 Replies

Avatar

Community Advisor

"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]

 



Arun Patidar

Avatar

Community Advisor

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)

 

Rohan_Garg_0-1710942636346.png

The URL bounces in endless redirection from https://www.abc.com/#z to https://www.abc.com/%23z

Avatar

Community Advisor

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.



Esteban Bustamante

Avatar

Community Advisor

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.


The way it is behaving is the "#" is basically not being treated by the Apache. So the # still exists on the final URL but the problem is if you have a "/#string" in the URL ideally that should not impact the page and will be simply forwarded to the browser to interpret. However, in my case the request converts first to https://www.abc.com/#z (This should basically be treated as a homepage request and not be redirected) & then gets redirected to https://www.abc.com/%23z
So I think the root problem is this redirect which basically should not be happening! Is this understanding correct?
 

 

Avatar

Community Advisor

Hi @Rohan_Garg 
You are doing rewrite, not the redirect, if you do redirect # will be gone from the url.



Arun Patidar

Avatar

Community Advisor

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.



Arun Patidar

Avatar

Community Advisor

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!

Avatar

Correct answer by
Community Advisor

yes, you need to create a config in AEM and expose as json and based on entry, perform client side redirect.



Arun Patidar

Avatar

Community Advisor

"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/';
}

Avatar

Community Advisor

@arunpatidar - Thanks a lot for your support as always!

Avatar

Administrator

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



Kautuk Sahni

Avatar

Community Advisor

Hey @kautuk_sahni, I will be sure to mark this query resolved once done! Thanks!