.htaccess htaccess mod rewrite$\u使用斜杠获取变量
我想用htaccess重写URL,使其可读性更好,并在PHP中使用$\u GET变量.htaccess htaccess mod rewrite$\u使用斜杠获取变量,.htaccess,mod-rewrite,url-rewriting,get,subdomain,.htaccess,Mod Rewrite,Url Rewriting,Get,Subdomain,我想用htaccess重写URL,使其可读性更好,并在PHP中使用$\u GET变量 我有时会使用子域,因此它必须使用和不使用。url中也不需要变量。我在URL中最多使用3个变量 URLsub.mydomain.com/page/a/1/b/2/c/3应该指向sub.mydomain.com/page.php?a=1&b=2&c=3,而URLsub.mydomain.com/a/1/b/2/c/3应该指向sub.mydomain.com/index.php?a=1&b=2&c=3,其中 我是在寻
我有时会使用子域,因此它必须使用和不使用。url中也不需要变量。我在URL中最多使用3个变量 URL
sub.mydomain.com/page/a/1/b/2/c/3
应该指向sub.mydomain.com/page.php?a=1&b=2&c=3
,而URLsub.mydomain.com/a/1/b/2/c/3
应该指向sub.mydomain.com/index.php?a=1&b=2&c=3
,其中
我是在寻找和尝试了很多之后才想到这个的
RewriteEngine on
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4&$5=$6&$7=$8 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3&$4=$5&$6=$7 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4&$5=$6 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3&$4=$5 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)$ $1.domain.com/$2.php [L,QSA,NC]
但我得到的是一个找不到的服务器错误
我在这方面不太在行,所以我可能会监督一些事情。
此外,我想它的工作与否,在结束斜杠
我应该使用RewriteCond和/或设置一些选项吗
提前感谢。当使用重写规则时,您不在行中包括域名。此外,请确保先打开发动机。像这样:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)$ index.php?$1=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?$1=$2&$3=$4
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?$1=$2&$3=$4&$5=$6
第一行将sub.mydomain.com/a/1
重写为sub.mydomain.com/page.php?a=1
,第二行将sub.mydomain.com/a/1/b/2
重写为sub.mydomain.com/page.php?a=1&b=2
,依此类推。是的,非常感谢!我那糟糕的引擎被打开了。我将编辑问题并将其放入代码块中。与其在mod_rewrite中进行此拆分,为什么不使用一个简单的catchall规则(RewriteRule^(.*)$index.php?p=$1
),然后在php中以您喜欢的方式解析url呢。大多数CMSE和web框架都是这样做的,因为它更灵活,更易于维护,并且不依赖Apache。@LieRyan,然后最多9个变量,好主意,下次遇到同样的问题时我会尝试。我忍不住说。。。Q舞FTW!