Home > Enterprise >  How can I convert % to # in dynamic URL via htacesss
How can I convert % to # in dynamic URL via htacesss

Time:01-18

Our site's links are like this:

https://www.example.com/video.html#11

But I don't know why, sometimes some sites convert our hash like this:

https://www.example.com/video.html

So I tried this line, it works but links are dynamic-based, because the ID of the video may change so I need to modify it.

ErrorDocument 404 https://www.example.com/video.html#11

And also, I have the following line in the .htaccess file but it doesn't work:

RewriteRule ^([^\.] )$ $1.html [NC,L]

CodePudding user response:

https://www.example.com/video.html

You can redirect this (and similar) URLs to replace % with # using something like the following mod_rewrite rule at the top of your .htaccess file:

RewriteCond %{THE_REQUEST} ^GET\s(/video\.html)%(\d )\s
RewriteRule . %1#%2 [NE,R=302,L]

THE_REQUEST contains the first line of the request headers, is not %-decoded and is not affected by other rewrites.

The %1 and %2 backreferences in the substitution string contain /video.html and the number after the % sign respectively.

The NE (noescape) flag is required to prevent the # sign being URL-encoded (as #) in the response and being interpreted as part of the URL-path.

HOWEVER, there are further complications with this as it depends on what ID numbers you are expecting after %. %NN (the first 2 digits) are obviously seen as a %-encoded character in the URL. The browser will convert some of these %-encoded characters back into the literal character before making the request, so %NN may not reach your server. Notably, this affects the standard latin characters a(a) to

  •  Tags:  
  • Related