need to escape # (hash/pound) character in .htaccess rewrite rule

Deadly 提交于 2019-11-29 09:18:40

The hash part of a URL is not available for rewriting. When a web browser sends a URL request to a web server it sends everything up to the hash sign. The hash is only available on the client (e.g. JavaScript code can see it).

I just got this working for a site following a couple of posts on this forum, I'm using a rewrite rule with NE not escape and R=301 redirect options:

RewriteRule ^galleries/([a-zA-Z0-9_-]+)$ /gallery.html#/$1 [R=301,NE,L]

This redirects all galleries/variable to /gallery.html#/variable

Edit: The important part of the rule is NE which instructs the server to parse output without escaping characters. Without this, it will try and escape the # in the rewrite rule which is what the OP is asking about.

Search Extended Redirection in http://httpd.apache.org/docs/2.0/misc/rewriteguide.html . There is a nice solution for your question.

Patrik Ján

.htaccess

RewriteRule  old\.php redirect.php?url=http://example.com/new.php|hash [R=301,QSA,L]

redirect.php

<?php
    $new_url = str_replace("|", "#", $_GET['url']);
    header("Location: ".$new_url, 301);
    die;
?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!