htaccess mod 用斜杠将 $_GET 重写为变量 [英] htaccess mod rewrite $_GET to variables with slashes
问题描述
我想用 htaccess 将 URL 重写为更好的可读 URL 并在 PHP 中使用 $_GET 变量
我有时会使用子域,因此它必须使用和不使用.url 中也不需要变量.我在 URL 中最多使用 3 个变量
I would like to rewrite URL's with htaccess to better readable URL's and use the $_GET variable in PHP
I sometimes make use of a subdomain so it has to work with and without. Also are the variables not necessary in the url. I take a maximum of 3 variables in the URL
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
the URL sub.mydomain.com/page/a/1/b/2/c/3
should lead to sub.mydomain.com/page.php?a=1&b=2&c=3
and the url sub.mydomain.com/a/1/b/2/c/3
should lead to sub.mydomain.com/index.php?a=1&b=2&c=3
where $_GET['a'] = 1
我在搜索并尝试了很多之后想出了这个
I came up with this after searching and trying a lot
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]
但我得到的是一个未找到的服务器错误
but what I get is an not found server error
我不太擅长这个,所以也许我会监督一些事情.
我也希望它在末尾有斜线和没有斜线
I'm not that good at this so maybe I oversee something.
Also I would like it to work with and without a slash at the end
我应该使用 RewriteCond 和/或设置一些选项吗?
Should I make use of RewriteCond and/or set some options?
提前致谢.
推荐答案
使用 RewriteRule
时,不要在行中包含域名.另外,请确保先打开 RewriteEngine.像这样:
When using RewriteRule
, you don't include the domain name in the line. Also, make sure you turn on the RewriteEngine first. Like this:
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
,依此类推.
The first line will rewrite sub.mydomain.com/a/1
to sub.mydomain.com/page.php?a=1
, the second rewrites sub.mydomain.com/a/1/b/2
to sub.mydomain.com/page.php?a=1&b=2
, and so on.
这篇关于htaccess mod 用斜杠将 $_GET 重写为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!