首页 > 解决方案 > Rewrite rule accepting #

问题描述

I have the current RewriteRule:

RewriteRule ^search/([0-9a-zA-Z-\s-:]+)(/?)$ search.php?search=$1 [NC,L,NE]

I'm wanting to have an url like this /search/#ff2266/, to accept hex (with the #) colors in the url and then have this value in the $_GET['search'] variable. But not only this, but I'm having problem with the #.

Is there any way i could do something like this: ([#0-9a-zA-Z-\s-:]+) ?

I've tried the rule:

RewriteRule ^search/(.*)(/?)$ search.php?search=$1 [NC,L,NE]

And then in the search.php the following code:

echo "var_dump($_GET): ";
var_dump($_GET);

and with the url /search/#f44336/ the result is:

var_dump(Array): array(0) { }

标签: regexapache.htaccess

解决方案


请参阅,# 之后的 URI 部分称为“片段”,并且根据定义仅在客户端可用/处理(请参阅https://en.wikipedia.org/wiki/Fragment_identifier)。

在客户端,可以使用带有 window.location.hash 的 javaScript 来访问它。

如果您想在搜索文件中访问该数字,您可以将其传递到那里而不使用哈希字符并以这种方式使用它。


推荐阅读