【问题标题】:htaccess - pretty url with multiple variableshtaccess - 带有多个变量的漂亮 url
【发布时间】:2021-07-01 18:30:48
【问题描述】:
<a href="sky.php?c=something&id=9&s=anything">sky</a>

something, 9, anything 是变量
在地址栏中我想看到这个:
...host/something/9/anything 所以 - 根本没有sky.php

反向规则 - 从漂亮到丑陋 - 已解决 - 但如果您有任何通知 - 请这样做:

RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /sky.php?c=$1&id=$2&s=$3 [L]

【问题讨论】:

  • 欢迎来到 SO 并感谢您的努力,所以这意味着您在浏览器中点击 http://localhost:80/sky.php?c=something&amp;id=9&amp;s=anything 之类的网址对吗?
  • @RavinderSingh13 - 嗯...我在 localhost 上有这个站点,但 .htaccess 文件只在远程服务器上,所以所有规则都应该对 example.com/sky.php... 有效
  • 好的,您能否确认您要点击以下网址:http://localhost:80/sky.php?c=something&amp;id=9&amp;s=anything,应在浏览器中更改为http://localhost:80/something/9/anything,但仍应在后端由http://localhost:80/sky.php?c=something&amp;id=9&amp;s=anything 提供服务?我在吗,请确认一次。
  • @RavinderSingh13 - 如果我不清楚,请原谅。请忘记本地主机。 .htaccess 仅在远程服务器上。您的代码中的所有其他内容都是正确的,只需将 localhost:80 替换为 example.com
  • 你好维加斯,我已经在一个答案中回复了这个问题,你可以在答案的评论部分评论更多关于它的讨论,谢谢。

标签: apache .htaccess mod-rewrite url-rewriting friendly-url


【解决方案1】:

使用您展示的示例,请尝试以下操作。这考虑到您想在浏览器中点击如下 URL:http://localhost:80/sky.php?c=something&amp;id=9&amp;s=anything,应在此处重定向到 http://localhost:80/something/9/anything

请确保在测试您的 URL 之前清除您的浏览器缓存。

RewriteEngine ON
##This rule handles to change from non-friendly URL to friendly URL in browser.
RewriteCond %{THE_REQUEST} \s/(?:[^.]*)\.php\?c=([^&]*)&id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

##This rule internally rewrites friendly url to non-friendly url.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ sky.php?c=$1&id=$2&s=$3  [L]

【讨论】:

  • 出了点问题。结果地址栏为example.com/sky/something/9
  • 如果您测试了代码 - 问题可能是我这边未知的问题。所以,无论如何 - 非常感谢,我会看看可能是什么原因。
  • 一些新的东西 - 我删除了一个名为 _htaccess_old 的旧文件,现在 url 字符串没问题,但仍然有 err 404
  • 简而言之 - 解决了。有一个小错误非常感谢您的时间和努力
  • 你的代码100%正确,我重命名文件时出错了,对不起
猜你喜欢
  • 1970-01-01
  • 2014-09-24
  • 2018-04-06
  • 2012-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
  • 1970-01-01
相关资源
最近更新 更多