【问题标题】:How to process external url as different internally using htaccess rewriterule?如何使用 htaccess rewriterule 在内部处理不同的外部 url?
【发布时间】:2020-01-18 15:44:19
【问题描述】:

新编辑:在我的 wordpress localhost 站点中,使用 .htaccess 我试图告诉服务器将https://example.com/store/foobar123 格式的链接处理为https://example.com/store/product.php?p=foobar123 [以便服务器能够处理product.php 的 php 代码的输出获取参数foobar。请注意,访问者继续在他的地址栏中看到https://example.com/store/foobar123,但服务器应该正确在内部将此网址处理为https://example.com/store/product.php?p=foobar123

我是 htaccess 的绝对初学者。我在下面有限的理解中尝试了以下和许多其他代码,但它不起作用/给出预期的结果:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^store/(.*)$ /store/product.php?p=$1 [L,QSA,NC]

htaccess 代码应该是什么,以便访问者在地址栏中打开链接 https://example.com/store/foobar123 时将继续在那里看到 https://example.com/store/foobar123 但服务器会在内部将此请求正确解释为 https://example.com/store/product.php?p=foobar123并通过抓取参数p的值foobar在文件product.php中显示代码的php输出,并在页面上向访问者显示结果

*New more edit 1 * : 我也试过下面的代码

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^store/(.*)$ store/product.php?p=$1 [QSA,L]

它不起作用,如果我访问 https://example.com/store/foobar123 它会给出错误 403 访问被禁止/找不到页面

但是如果我访问https://example.com/store/product.php?p=foobar123,那么这个链接会正常打开,所以我认为这意味着我在上面的 htaccess 代码中犯了一些错误。我该如何解决它

*New more edit 2 * : 我也尝试了下面的代码使用这个(教程)[https://httpd.apache.org/docs/current/rewrite/remapping.html#old-to-new]。如果我打开https://example.com/store/foobar123,它还会给出访问禁止错误/找不到页面错误。请注意,如果我直接打开https://example.com/store/product.php?p=foobar123,我不会收到错误消息

RewriteEngine on
RewriteRule "^/store/(.*)$"  "/store/product.php?p=$1" [PT]

【问题讨论】:

  • “当访问者将鼠标悬停在链接上时,它应该看起来像 https://example.com/store/foobar123 - 然后您必须注意系统输出的 HTML 代码包含那个版本开始。这部分问题不是 mod_rewrite 的责任。
  • @04FS,我在 foo 前面添加 https://example.com/store/product.php?p= 使用 php。如果您要编辑我的 wordpress 帖子,您只会在其中找到 foo 的解密版本。我在其他网站上看到过,我只是不知道他们如何能够隐藏product.php?p=foo 也是其他 url 的加密版本
  • 我不明白你的意思,但这也无所谓。如果您希望状态栏在我将鼠标悬停在链接上时显示 https://example.com/store/foobar123,那么 that 必须是该链接的 href 属性包含的 URL。
  • @04FS 所以你的意思是我必须使用加密来隐藏product.php?p=?正如你所说,mod_rewrite 不会做我需要的。我只想对访问者隐藏/混淆product.php?p=
  • 不,您必须首先创建您的 HTML,输出您希望网站访问者首先看到的正确 URL。您的 HTML 需要链接到 https://example.com/store/foobar123 - 这是用户将看到的内容,以及将发送到服务器的请求。在服务器端,这将被重写为/store/product.php?p=…internally,以供进一步处理。这是一个两步过程,您需要在两个地方进行更改。

标签: .htaccess url-rewriting


【解决方案1】:

如果原始 URL 格式是 https://example.com/store/product.php?p=foobar123,而您想将其更改为 https://example.com/store/foobar123,那么这里是您可以在 public_html/.htaccess 中使用的规则

RewriteEngine on
RewriteCond %{THE_REQUEST} /store/product\.php\?p=([^\s]+) [NC]
RewriteRule .+ /store/%1? [L,R]
RewriteRule ^store/((?!product\.php).+)/?$ /store/product.php?p=$1 [L,NC]

这会将您的新网址映射到旧网址。

【讨论】:

  • 没用,url还是显示为https://example.com/store/product.php?p=foobar123
  • 谢谢你,但它仍然是一样的,如果这有帮助,则显示为https://example.com/store/product.php?p=foobar123 idk,但我在 localhost wordpress 上
  • @simon 只需将此代码放在 WordPress 规则之前的 htaccess 顶部,否则这些规则会覆盖它。
  • @simon 抱歉回复晚了。实际上,如果您键入原始 URL,这应该可以工作,然后将其转换为新的短格式。例如,/store/product.php?p=foobar 将更改为 /store/foobar 并显示原始文件的输出。您能否详细说明您在使用此规则时遇到的问题?当您在浏览器中输入 URL 时会发生什么?
  • 我已根据您的回复正确更改了问题,请您再检查一下。将代码放入 .htaccess 后,如果我将鼠标悬停在超链接上,它会显示为 https://example.com/store/product.php?p=foobar123,但如果我在浏览器中打开此链接,它会以 https://example.com/store/foobar123 的形式打开并给出错误找不到对象。我在 localhost/testsite 上对其进行测试,所以原始链接看起来像 http://localhost/testsite/store/product.php?p=foobar123 而新链接看起来像 http://localhost/testsite/store/foobar123 但它给了我找不到错误对象
猜你喜欢
  • 2012-09-04
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 2018-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多