.htaccess htaccess mod将$_GET重写为带有斜杠的变量

os8fio9y  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(128)

我想用htaccess重写URL,使其更容易阅读,并在PHP中使用$_GET变量
我有时会使用一个子域,所以它必须工作与和没有。也是变量不必要的网址。我采取了最多3个变量的网址
URL sub.mydomain.com/page/a/1/b/2/c/3应指向sub.mydomain.com/page.php?a=1&b=2&c=3,URL sub.mydomain.com/a/1/b/2/c/3应指向sub.mydomain.com/index.php?a=1&b=2&c=3,其中$_GET['a'] = 1
我在寻找和尝试了很多之后想到了这个

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和/或设置一些选项吗?
先谢谢你。

8aqjt8rx

8aqjt8rx1#

当使用RewriteRule时,不要在行中包含域名。另外,确保先打开RewriteEngine。如下所示:

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,依此类推。

相关问题