mod_rewrite 和 php 变量

发布于 2024-09-16 09:10:51 字数 440 浏览 5 评论 0原文

我正在尝试让 mod_rewrite 与我的网站一起使用,但由于某种原因它不起作用。 我已经在我的 .htaccess 文件中输入代码以将非 www 重定向到 www,所以我知道 mod_rewrite 总体上正在工作。

我试图更改的网址是 example.com/index.php?p=home 所以新的网址将是 example.com/page/home

但是,当我尝试这个代码,我只是得到一个 404 告诉我 /page/home 不存在。

Options +FollowSymLinks

RewriteEngine on

RewriteRule index/p/(.*)/ index.php?p=$1
RewriteRule index/p/(.*) index.php?p=$1

有人可以帮我吗?

I am trying to get mod_rewrite to work with my site but for some reason it's not working.
I've already entered code into my .htaccess file to redirect non-www to www so I know mod_rewrite is working in general.

The url's I'm trying to change are example.com/index.php?p=home so the new URL would be example.com/page/home

However, when I try this code I simply get a 404 telling me that /page/home doesn't exist.

Options +FollowSymLinks

RewriteEngine on

RewriteRule index/p/(.*)/ index.php?p=$1
RewriteRule index/p/(.*) index.php?p=$1

Can anyone help me out please?

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

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

发布评论

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

评论(2

亣腦蒛氧 2024-09-23 09:10:51

/page/xxxx

您的重写规则使用index/p/xxxxx,但您想尝试

RewriteRule ^/page/(.*)/ index.php?p=$1
RewriteRule ^/page/(.*) index.php?p=$1

Your rewrite rule uses index/p/xxxxx but you want /page/xxxx

try

RewriteRule ^/page/(.*)/ index.php?p=$1
RewriteRule ^/page/(.*) index.php?p=$1
懒猫 2024-09-23 09:10:51

您的模式与您的示例网址不匹配。假设您的示例 URL 是正确的,您需要的是:

Options +FollowSymLinks

RewriteEngine on

# We want to rewrite requests to "/page/name" (with an optional trailing slash)
# to "index.php?p=name"
#
# The input to the RewriteRule does not have a leading slash, so the beginning
# of the input must start with "page/". We check that with "^page/", which
# anchors the test for "page/" at the beginning of the string.
#
# After "page/", we need to capture "name", which will be stored in the
# backreference $1. "name" could be anything, but we know it won't have a
# forward slash in it, so check for any character other than a forward slash
# with the negated character class "[^/]", and make sure that there is at least
# one such character with "+". Capture that as a backreference with the
# parenthesis.
#
# Finally, there may or may not be a trailing slash at the end of the input, so
# check if there are zero or one slashes with "/?", and make sure that's the end
# of the pattern with the anchor "$"
#
# Rewrite the input to index.php?p=$1, where $1 gets replaced with the
# backreference from the input test pattern
RewriteRule ^page/([^/]+)/?$ index.php?p=$1

Your pattern doesn't match your example URL. Assuming your example URL was correct, you wanted this instead:

Options +FollowSymLinks

RewriteEngine on

# We want to rewrite requests to "/page/name" (with an optional trailing slash)
# to "index.php?p=name"
#
# The input to the RewriteRule does not have a leading slash, so the beginning
# of the input must start with "page/". We check that with "^page/", which
# anchors the test for "page/" at the beginning of the string.
#
# After "page/", we need to capture "name", which will be stored in the
# backreference $1. "name" could be anything, but we know it won't have a
# forward slash in it, so check for any character other than a forward slash
# with the negated character class "[^/]", and make sure that there is at least
# one such character with "+". Capture that as a backreference with the
# parenthesis.
#
# Finally, there may or may not be a trailing slash at the end of the input, so
# check if there are zero or one slashes with "/?", and make sure that's the end
# of the pattern with the anchor "$"
#
# Rewrite the input to index.php?p=$1, where $1 gets replaced with the
# backreference from the input test pattern
RewriteRule ^page/([^/]+)/?$ index.php?p=$1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文