使用当前文件夹中的参数通过 .htaccess 重定向到 .php

发布于 2024-10-02 15:03:46 字数 349 浏览 0 评论 0原文

我正在尝试将 foo/bar 之类的内容重定向到 ?foo=bar,这样我就可以执行 www.mydomain.com/hey/foo/bar< /code> 到 www.mydomain.com/hey/?foo=bar,但我似乎无法正确理解语法。我尝试了以下方法:

RewriteEngine on
RewriteRule ^foo/(.*)$ ?foo=bar [NC]

但这不起作用。我将如何实现这个目标?我尝试在问号后面添加一个正斜杠,但这使它链接到根目录。

谢谢, 詹格勒

I'm trying to redirect something like foo/bar to ?foo=bar, so I can do www.mydomain.com/hey/foo/bar to www.mydomain.com/hey/?foo=bar, but I can't seem to get the syntax right. I tried the following:

RewriteEngine on
RewriteRule ^foo/(.*)$ ?foo=bar [NC]

But this doesn't work. How would I accomplish this? I tried adding a forward slash behind the question mark, but that makes it link to the root directory.

Thanks,
Jengerer

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

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

发布评论

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

评论(4

爱要勇敢去追 2024-10-09 15:03:46

您需要包含要重写的实际文件。使用 ?foo=bar 并不指向任何特定文件。

使用以下规则:

RewriteEngine on
RewriteRule ^(.*)/(.*)$ index.php?$1=$2 [NC]

请注意,我指向文件 index.php$1 被第一个 (.*) 匹配的内容替换,$2 被第二个 (.*) 匹配的内容替换。因此,如果有人浏览 foo/bar,他们将被带到 index.php?foo=bar

重要说明:如果您选择使用 (.*) 并接受变量名称或值的任何字符,并计划在数据库查询中使用此信息,您将需要确保使用数据库的转义函数(mysql_real_escape_stringpg_escape_string)或使用准备好的语句正确转义此内容。

如果由于使用相对路径而导致页面上的样式或元素无法正确显示,则需要使用从根开始的绝对路径。否则,您的页面样式、图像等将会损坏。

<link rel="stylesheet" type="text/css" href="/path/to/style.css" />

You need to include the actual file you're rewriting to. Using ?foo=bar isn't pointing at any file in particular.

Use the following rule:

RewriteEngine on
RewriteRule ^(.*)/(.*)$ index.php?$1=$2 [NC]

Notice that I'm pointing to the file index.php. $1 is replaced by whatever is matched by the first (.*) and $2 by the second. So, if someone browsed to foo/bar, they would be taken to index.php?foo=bar.

Important Note: If you choose to use (.*) and accept any character for the variable name or value and plan to use this information in database queries, you'll want to be certain to escape this content properly using your database's escape functions (mysql_real_escape_string or pg_escape_string) or by using prepared statements.

If you're having problems with style or elements on the page not showing correct because you're using relative paths, you'll need to use absolute paths starting at the root. Otherwise, your pages style, images, etc. would break.

<link rel="stylesheet" type="text/css" href="/path/to/style.css" />
花之痕靓丽 2024-10-09 15:03:46

也许尝试从 ^foo/(.*)$ 中删除 ^ ?

Maybe try removing the ^ from ^foo/(.*)$?

∞梦里开花 2024-10-09 15:03:46

事实证明,问题不在于重定向,而在于重定向后建立的链接。

/foo/bar 重定向到 /?foo=bar 后,CSS 链接和图像匹配不佳,因为它现在正在 / 中查找本地链接foo/bar 目录,该目录不存在。多么有趣的“小故障”啊。

Well, it turns out that the problem wasn't in the redirection, but in the links that were made after the redirection.

After redirecting from /foo/bar to /?foo=bar, the CSS links and images were poorly matched because it was now looking for local links within the /foo/bar directory, which doesn't exist. What an interesting 'glitch'.

你不是我要的菜∠ 2024-10-09 15:03:46

当您将 .htaccess 放在根目录中时,它应该是这样的:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\/]+)\/([^\/]+)\/([^\/]+)$ $1?$2=$3 [L]

当您将 .htaccess 放在“hey”文件夹中时,它应该是这样的:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]+)\/([^\/]+)$ ?$1=$2 [L]

When you place the .htaccess in your root it should be like this:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\/]+)\/([^\/]+)\/([^\/]+)$ $1?$2=$3 [L]

when you place the .htaccess in your "hey" folder, it should be like this:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]+)\/([^\/]+)$ ?$1=$2 [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文