[英]Apache rewrite rules doesn't work only with particular prefix
I've some problem with a script I'm writing. 我正在编写的脚本有问题。
I have those kind of URLs: 我有这类网址:
forum.php?f=topic&g=$1&id=$2&alias=$3
forum.php?f=group&g=$1
forum.php
I need to rewrite those for have: 我需要重写那些为:
/forum/group/id-alias_topic > forum.php?f=topic&g=[GROUP]&id=[ID]&alias=[ALIAS_TOPIC]
/forum/group > forum.php?f=group&g=[GROUP]
/forum > forum.php
I tried with: 我尝试了:
RewriteRule ^forum/([\w'-]+)/([0-9]+)-([\w'-]+) forum.php?f=topic&g=$1&id=$2&alias=$3
RewriteRule ^forum/([\w'-]+) forum.php?f=group&g=$1
RewriteRule ^forum/ forum.php
But it doesn't work. 但这是行不通的。 It shows me only forum.php 它只向我显示forum.php
Every URL starting with /forum
shows me the default page provided by /forum.php
每个以/forum
开头的URL都会向我显示/forum.php
提供的默认页面
For example, forum.php
will show the text "MAIN PAGE". 例如, forum.php
将显示文本“ MAIN PAGE”。
forum.php?f=topic&g=[GROUP]&id=[ID]&alias=[ALIAS_TOPIC]
should show me "GROUP + ID + ALIAS_TOPIC" forum.php?f=topic&g=[GROUP]&id=[ID]&alias=[ALIAS_TOPIC]
应该告诉我“ GROUP + ID + ALIAS_TOPIC”
But if I visit /forum/android/1-first_topic
it shows me "MAIN PAGE" 但是,如果我访问/forum/android/1-first_topic
则会显示“主页面”
If I replace ^forum
with, for example ^foru
, it works. 如果我将^forum
替换为^foru
,则它可以工作。 I've cleaned my browser cache and restarted apache, but it still doesn't work. 我已经清理了浏览器缓存并重新启动了Apache,但是仍然无法正常工作。 Also with other browsers the problem is the same. 同样对于其他浏览器,问题也相同。 In my /var/www there are those files and directories: 在我的/ var / www中,有那些文件和目录:
administrator.php assets cache forum_functions.php forum.php functions.php global.php index.php media notfound.php OLD pwdgen.php robots.txt rss.php simple_html_dom.php store.php template v.php
Do you have some advice? 你有什么建议吗?
Rewrite.log: http://pastebin.com/MeapYeBA Rewrite.log: http ://pastebin.com/MeapYeBA
Because you have not used the [L]
(last) flag and not anchored the end of the expressions with $
each one matches the last rule after dropping through the first two. 因为您没有使用[L]
(最后一个)标志,也没有用$
锚定表达式的末尾,所以在删除前两个后,每个匹配最后一条规则。
For example, this URL will match both the first and last rules: example.com/forum/group/123-thing
because the last rule matches forum/
and anything that follows it if not terminated by $
. 例如,此URL将同时匹配第一个和最后一个规则: example.com/forum/group/123-thing
因为最后一个规则匹配forum/
及其后面的任何内容(如果没有以$
终止)。
Add [L]
flags to prevent fall-through, and terminate the regex with $
添加[L]
标志以防止掉线,并使用$
终止正则表达式
# Don't apply these if the file actually exists (like forum.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# This one doesn't really need to terminate in $
RewriteRule ^forum/([^/]+)/([0-9]+)-(.+) forum.php?f=topic&g=$1&id=$2&alias=$3 [L]
# This one must terminate with $
RewriteRule ^forum/([^/]+)$ forum.php?f=group&g=$1 [L]
# As must this one..
RewriteRule ^forum/?$ forum.php [L]
I have also replaced your [\\w'-]+
with a simpler [^/]+
to match everything up to the next /
. 我还用一个更简单的[^/]+
替换了您的[\\w'-]+
,以匹配到下一个/
为止的所有内容。 In the last rule, I added /?
在最后一条规则中,我添加了/?
to allow for an optional trailing /
. 允许可选的尾随/
。 It will work just as well with [\\w'-]+
, but since you intend to match everything between /
, [^/]+
is a more common pattern. 它与[\\w'-]+
,但是由于您打算匹配/
之间的所有内容,因此[^/]+
是更常见的模式。
Each of the above rules is tested and working over at http://htaccess.madewithlove.be/ 以上每个规则都经过测试,可以在http://htaccess.madewithlove.be/上进行操作。
You Must put skip flag for stop other rules With tihs [S] at end of RewriteRule like 必须在RewriteRule的末尾加上tihs [S]来停止其他规则以使其跳过,例如
RewriteRule ^forum/([\w'-]+)/([0-9]+)-([\w'-]+)$ forum.php?f=topic&g=$1&id=$2&alias=$3 [S=2]
RewriteRule ^forum/([\w'-]+)$ forum.php?f=group&g=$1 [S=1]
RewriteRule ^forum/$ forum.php
Found a solution thanks to @Michael Berkowski @Michael Berkowski找到了解决方案
sudo nano /etc/apache2/sites-enabled/000-default
and remove all occurrences of MultiViews, then restart apache. 并删除所有出现的MultiViews,然后重新启动apache。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.