Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess Htaccess重写第二条规则不起作用_.htaccess_Mod Rewrite_Url Rewriting - Fatal编程技术网

.htaccess Htaccess重写第二条规则不起作用

.htaccess Htaccess重写第二条规则不起作用,.htaccess,mod-rewrite,url-rewriting,.htaccess,Mod Rewrite,Url Rewriting,我想把像index.php?c=4&index.php?g=23这样的URL重写到website.com/games/categoryname/id/ 对于游戏页面website.com/play/gamename/id/ 我的htaccess文件如下所示: Options +FollowSymlinks RewriteEngine on RewriteRule ^([a-zA-Z0-9\-_%]+)/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?c=$3 Re

我想把像index.php?c=4&index.php?g=23这样的URL重写到website.com/games/categoryname/id/ 对于游戏页面website.com/play/gamename/id/

我的htaccess文件如下所示:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9\-_%]+)/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?c=$3
RewriteRule ^([a-zA-Z0-9\-_%]+)/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?g=$3
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^play/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?c=$2
RewriteRule ^games/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?g=$2
问题是,只有第一个重写规则起作用,如果我对它进行注释,那么第二个规则也会起作用,但决不能两者都起作用:(.我正在MAMP上测试这一点


您能帮助我吗?

它们不能同时工作,因为它们具有相同的条件-您使用相同的条件设置了两个不同的操作,并且只执行第一个操作

啊,我理解你想要实现的目标:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(games)/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?c=$3 [L]
RewriteRule ^(play)/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?g=$3 [L]

它们不能同时工作,因为它们具有相同的条件-您使用相同的条件设置了两个不同的操作,并且只执行第一个操作

啊,我理解你想要实现的目标:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(games)/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?c=$3 [L]
RewriteRule ^(play)/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?g=$3 [L]

你必须这样做:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9\-_%]+)/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?c=$3
RewriteRule ^([a-zA-Z0-9\-_%]+)/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?g=$3
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^play/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?c=$2
RewriteRule ^games/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?g=$2

你必须这样做:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9\-_%]+)/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?c=$3
RewriteRule ^([a-zA-Z0-9\-_%]+)/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?g=$3
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^play/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?c=$2
RewriteRule ^games/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?g=$2

在每个
重写规则
@Alexander Ivanov之后放置
[L]
(最后一个)是个好主意。我同意,这是一个很好的做法,尽管你可能希望在这些规则之后有更多的重写规则,所以这取决于具体情况。如果他在这些规则之后放置一个规则来匹配用户名,例如
RewriteRule^(+)$index.php?user=$1
-那会搞得一团糟……我想的更多的东西是
重写规则(.*)/index.php$1/
,尽管有人可能会说这应该放在其他规则之前。最好放一个
[L]
(最后一个)每次
重写规则之后
@Alexander Ivanov我同意,这是一个很好的做法,尽管你可能希望在这些规则之后有更多的重写规则,所以这取决于具体情况。如果他在这些规则之后放置一个与用户名匹配的规则,例如
重写规则^(.+)$index.php?user=$1
-那会搞得一团糟……我想的更多的是类似于
重写规则(.*)/index.php$1/
,尽管有人可能会说这应该放在其他规则之前。