Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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_Url_Redirect_Mod Rewrite_Url Rewriting - Fatal编程技术网

.htaccess 仅通过htaccess为特殊格式自定义重定向

.htaccess 仅通过htaccess为特殊格式自定义重定向,.htaccess,url,redirect,mod-rewrite,url-rewriting,.htaccess,Url,Redirect,Mod Rewrite,Url Rewriting,你好,我想通过htaccess自定义重定向 仅适用于此格式 www.example.com/index.php?id=abc --> www.example.com/abc 但对于任何其他格式,无需更改: www.example.com/index.php?id=abc&id2=qaz&id3=wsx --> www.example.com/index.php?id=abc&id2=qaz&id3=wsx 比如说 www.example.com/

你好,我想通过htaccess自定义重定向

仅适用于此格式

www.example.com/index.php?id=abc --> www.example.com/abc
但对于任何其他格式,无需更改:

www.example.com/index.php?id=abc&id2=qaz&id3=wsx --> www.example.com/index.php?id=abc&id2=qaz&id3=wsx 
比如说

www.example.com/index.php?id=abc&id2=qaz --> www.example.com/index.php?id=abc&id2=qaz
www.example.com/index.php?id=abc --> www.example.com/abc

www.example.com/index.php?id=abc&id2=qaz --> www.example.com/abc/qaz
此代码(由@starkeen编写)非常有用:

RewriteEngine on

#1)Redirect "/index.php?id=foo" to "/foo"#
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+)\sHTTP [NC]
RewriteRule ^ /%1? [L,R]
#2)The rule bellow will internally map "/foo" to "/index.php?id=foo"#
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /index.php?id=$1 [L]
现在如何添加自定义参数名称,如id2(或更多)

比如说

www.example.com/index.php?id=abc&id2=qaz --> www.example.com/index.php?id=abc&id2=qaz
www.example.com/index.php?id=abc --> www.example.com/abc

www.example.com/index.php?id=abc&id2=qaz --> www.example.com/abc/qaz
但是(这里)对于这一点没有变化:

www.example.com/index.php?id=abc&id2=qaz&id3=wsx --> www.example.com/index.php?id=abc&id2=qaz&id3=wsx 

您需要另一组规则来处理2个查询参数:

RewriteEngine On

#1A) Redirect "/index.php?id=foo" to "/foo"
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+)\sHTTP [NC]
RewriteRule ^ /%1? [L,R]

#1B) Redirect "/index.php?id=foo&id2=bar" to "/foo/bar"
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+)&id2=([^\s&]+)\sHTTP [NC]
RewriteRule ^ /%1/%2? [L,R]

# skip all the files and directories from further rules
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

#2A) The rule bellow will internally map "/foo/bar" to "/index.php?id=foo&id2=bar"
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?id=$1&id2=$2 [L,QSA]

#2B) The rule bellow will internally map "/foo" to "/index.php?id=foo"
RewriteRule ^([^/]+)/?$ /index.php?id=$1 [L,QSA]