Redirect URLs with a # in .htaccess

Redirect URLs with a # in .htaccess - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the Redirect URLs with a # in .htaccess error on your web browser. Problem :


On one of my sites I get 404s because some scripts are fetching all URLs from the start page as raw URLs, so they include the # in the URL. Normal browsers don’t ever send that part to the server, but these scripts do.



This is how a typical requests URL looks like:



/2014/how-to-manage-wordpress-multisite-imports-with-wp-cli/#comments


The # is not URL encoded.



I tried both following methods:



RedirectMatch 301 #comments       /


and



RewriteRule #(.+)$ /? [L,R=301]


Both without success, the rules don’t catch these requests, because the # starts a comment. The referer and the user-agent fields are empty.



What should I do?


Solution :

From the mod_rewrite documentation you need to use the NE (no escape) flag when your rewrite rule has a hash:



RewriteRule #(.+)$ /? [L,R=301,NE]


You commented that the NE flag may only apply to the target URL and not the rewrite pattern. If that is the case, another approach would be to escape the # sign. mod_rewrite supports x style escape sequences. The escape sequence for # would be x23. So your rewrite rule could be:



RewriteRule x23.+$ / [L,R=301]


If you want to test a solution, you can do so with telnet on the command line. Use the command line telnet example.com 80 to open a socket to your webserver. Then make a simple request like this:



GET /#test HTTP/1.0
Host: example.com


Followed by an extra new line.


We hope that this article has helped you resolve the htaccess, redirects, hash error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

How to redirect to any domain [duplicate]

"302 found" for index page bad for SEO?

Create redirect from url like www.example.us/?p=100&option=