我想让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
有人可以帮帮我吗?
答案 0 :(得分:2)
您的重写规则使用index / p / xxxxx,但您需要/ page / xxxx
尝试
RewriteRule ^/page/(.*)/ index.php?p=$1
RewriteRule ^/page/(.*) index.php?p=$1
答案 1 :(得分:1)
您的模式与示例网址不符。假设您的示例网址是正确的,您需要这样做:
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