.htaccess RewriteRule 将目录类型 URL 结构转换为查询

发布于 2024-10-02 08:10:16 字数 833 浏览 0 评论 0原文

我知道这个问题已经被问/回答过好几次了,但我似乎无法克服它。所以,我很抱歉...

在网站开发阶段,我希望浏览器地址栏显示:domain.com/dev/item

我希望 RewriteRule 将其更改为:domain.com/dev/index.php?p=item

我使用位于 public_html 目录中的 .htaccess 来实现此目的。 public_html/dev 中的 .htaccess 工作正常,配置为将所有不使用我的 IP 的人重定向到 domain.com。我的 apache 版本是 2.2.15,我已经确认我能够使用 .htaccess 进行重定向,但顶级 .htacess 根本不起作用。这是我使用的代码:(

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^dev/(.*)$ dev/index.php?p=$1 [NC]
#variations tried: ^dev/(.*)+$, ^dev/(.*)+?$

包括变体)导致 404,/dev/item 未找到。从 ^dev/(.*)$ 中删除 dev/ 会产生正确的 url 格式,但会出现以下服务器变量:

[REDIRECT_QUERY_STRING] => p=
[REDIRECT_URL] => /404.shtml
[QUERY_STRING] => p=
[REQUEST_URI] => /dev/item

我的目标是进行一些小更改以支持实时站点和开发副本的这种类型的重写。但我就是不明白我做错了什么。我真的很感激任何帮助。

i know this question has been asked/answered several times already, but i just cant seem to get past it. so, my apologies...

while in the site dev phase, i want the browsers address bar to display: domain.com/dev/item

i would like the RewriteRule to change it to: domain.com/dev/index.php?p=item

im using the .htaccess located in the public_html directory for this. the .htaccess within public_html/dev, which works fine, is configured to redirect everybody who is not using my ip to domain.com. my apache version is 2.2.15 and i've confirmed that i am able to redirect using .htaccess, but the top level .htacess simply doesnt work. here is the code im using:

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^dev/(.*)$ dev/index.php?p=$1 [NC]
#variations tried: ^dev/(.*)+$, ^dev/(.*)+?$

the, including variations, results in a 404, /dev/item not found. removing the dev/ from ^dev/(.*)$ results in the correct url format, but the following server variables:

[REDIRECT_QUERY_STRING] => p=
[REDIRECT_URL] => /404.shtml
[QUERY_STRING] => p=
[REQUEST_URI] => /dev/item

my goal is to make a small change to support this type of rewrite for both the live site and the dev copy. but i just cant understand what im doing wrong. i really would appreciate any help.

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

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

发布评论

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

评论(1

塔塔猫 2024-10-09 08:10:16

您需要在重写路径的开头指定 / 。

试试这个:

RewriteEngine on
RewriteBase /

# If request is for a file that does actually exist, don't rewrite.
RewriteCond %{REQUEST_FILENAME} !-f
 # If request is for a directory that does actually exist, don't rewrite.
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^/dev/(.*)$ /dev/index.php?p=$1 [NC]

You need to specify the / at the beginning of the rewrite paths.

Try this instead:

RewriteEngine on
RewriteBase /

# If request is for a file that does actually exist, don't rewrite.
RewriteCond %{REQUEST_FILENAME} !-f
 # If request is for a directory that does actually exist, don't rewrite.
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^/dev/(.*)$ /dev/index.php?p=$1 [NC]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文