使用 mod rewrite 取消混淆 URL

发布于 2024-08-08 21:40:15 字数 1014 浏览 3 评论 0原文

我在一家公司工作,该公司过去对文章的 URL 进行混淆,需要注册才能使用。所以他们会有类似

/story.php?story_id=Z_ZXYZ

的东西,其中有一些代码将字母映射到数字以计算出真实的故事 ID

Z = 0 
Y = 1
X = 2 

我们现在正在移动技术堆栈,并决定不需要混淆的 url。所以我正在考虑是否可以使用 mod rewrite 来混淆 URL

到目前为止,我

  RewriteCond %{REQUEST_URI} ^.*story.php [NC]
  RewriteCond %{QUERY_STRING} ^.*story_id=Z_([ZXYWVUTSRQ]*) [NC]
  RewriteRule ^.*$ /story/${decryptmap:%1} [L,R=301]

在 httpd.conf 文件中有一个重写映射,

<IfModule mod_rewrite.c>
  RewriteMap decryptmap txt:decyrpsdstxt
</IfModule>

其中包含内容

##
## decrypt urls
##
Z 0
X 1

等..

但它似乎不起作用,即使我在重写规则中添加了一些文本 RewriteRule ^.*$ /story/${decryptmap:ZXY} [L,R=301] 我得到一个像 /story/?story_id 这样的网址=Z_ZAD

我有什么明显做错的地方吗?我可以看到这两个条件正在匹配,但地图似乎不起作用。

我是否应该尝试让 mod 重写来做到这一点?我可以重定向到一个脚本,该脚本相当容易地完成此操作,但这会将重定向代码放在我不喜欢的两个地方。

(我不担心?story_id=Z_ZAD,我知道如何摆脱它)

I work for a company that used to have a polocy of obfuscating URLs for articles which you needed to register to use. So they would have something like

/story.php?story_id=Z_ZXYZ

which there was some code which mapped letters to numbers to work out the true story id

so

Z = 0 
Y = 1
X = 2 

etc.

We are now moving technology stack and have decided that obfuscated urls are not needed. So I am looking at whether it is possible to un obfuscate the URLs using mod rewrite

So far I have

  RewriteCond %{REQUEST_URI} ^.*story.php [NC]
  RewriteCond %{QUERY_STRING} ^.*story_id=Z_([ZXYWVUTSRQ]*) [NC]
  RewriteRule ^.*$ /story/${decryptmap:%1} [L,R=301]

I have a rewrite map in the httpd.conf file

<IfModule mod_rewrite.c>
  RewriteMap decryptmap txt:decyrpsdstxt
</IfModule>

Which has content

##
## decrypt urls
##
Z 0
X 1

etc..

but it dosn't seem to be working, even if I put some text in the Rewriterule as so RewriteRule ^.*$ /story/${decryptmap:ZXY} [L,R=301] I get a url like /story/?story_id=Z_ZAD

Is there anything obvious I am doing wrong? I can see that the two conditions are being matched but the map doesn’t seem to work.

Should I even be trying to get mod rewrite to do this? I could redirect to a script which did this fairly easily but that would put redirect code in two places which I didn't like the idea of.

(I'm not worried about the ?story_id=Z_ZAD, I know how to get rid of that)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

む无字情书 2024-08-15 21:40:15

重写映射将处理已传递的整个字符串。因此,在您的情况下,您一次只需要传递一个字符:

# put story_id at the begin for efficiency
RewriteCond %{QUERY_STRING} ^(([^&]*&)+)(story_id=Z_[^&]*)&*(.*)$
RewriteRule ^story\.php$ story.php?%3&%1%4

# decrypt story_id value character by character
RewriteCond %{QUERY_STRING} ^story_id=Z_([^ZXYWVUTSRQ]*)([ZXYWVUTSRQ])(.*)
RewriteRule ^story\.php$ story.php?story_id=Z_%1${decryptmap:%2}%3 [N]

# redirect
RewriteCond %{QUERY_STRING} ^story_id=Z_([^&])&*(.*)
RewriteRule ^story\.php$ /story/%1?%2 [L,R=301]

A rewrite map would work with the whole string that has been passed. So in your case you would need to pass just one character at a time:

# put story_id at the begin for efficiency
RewriteCond %{QUERY_STRING} ^(([^&]*&)+)(story_id=Z_[^&]*)&*(.*)$
RewriteRule ^story\.php$ story.php?%3&%1%4

# decrypt story_id value character by character
RewriteCond %{QUERY_STRING} ^story_id=Z_([^ZXYWVUTSRQ]*)([ZXYWVUTSRQ])(.*)
RewriteRule ^story\.php$ story.php?story_id=Z_%1${decryptmap:%2}%3 [N]

# redirect
RewriteCond %{QUERY_STRING} ^story_id=Z_([^&])&*(.*)
RewriteRule ^story\.php$ /story/%1?%2 [L,R=301]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文