Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.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
Apache URL重定向,htaccess。如果找不到字符串,则捕获并在字符串后插入_Apache_.htaccess_Redirect_Mod Rewrite_Url Rewriting - Fatal编程技术网

Apache URL重定向,htaccess。如果找不到字符串,则捕获并在字符串后插入

Apache URL重定向,htaccess。如果找不到字符串,则捕获并在字符串后插入,apache,.htaccess,redirect,mod-rewrite,url-rewriting,Apache,.htaccess,Redirect,Mod Rewrite,Url Rewriting,我希望通过htaccess解决方案获得以下重定向的帮助: 案例1)用户进入domain.com,应重定向至domain.com/nl 案例2)如果用户输入domain.com/nl或domain.com/fr,则不应重定向 案例3)用户进入domain.com/some-path,它应该捕获url路径并将/nl粘贴在它前面,domain.com/nl/some-path 到目前为止,我尝试了以下规则: Redirect 301 ^((?!(?:)nl($)|(?:)fr($)).)*$ doma

我希望通过htaccess解决方案获得以下重定向的帮助:

案例1)用户进入domain.com,应重定向至domain.com/nl

案例2)如果用户输入domain.com/nl或domain.com/fr,则不应重定向

案例3)用户进入domain.com/some-path,它应该捕获url路径并将/nl粘贴在它前面,domain.com/nl/some-path

到目前为止,我尝试了以下规则:

Redirect 301 ^((?!(?:)nl($)|(?:)fr($)).)*$ domain.com/nl/$1
但是,在案例3中,仅选择路径的最后一个字符,而不是整个“某个路径”


使用多个规则也可以,但是可以在单个规则中完成吗?

可以在单个规则中完成,如下所示:

重新编写引擎打开
重写cond%{THE_REQUEST}\s/+(?:nl | fr)[/?\s][NC]
重写规则^/nl%{REQUEST_URI}[L,NE,R=301]

请您尝试以下内容,并使用显示的样本编写和测试。请确保在测试URL之前清除浏览器缓存

RewriteEngine ON
##For adding nl to home/base page.
RewriteRule ^/?$ http://%{HTTP_HOST}/nl [R=301,NE,L]

##For dealing with pages NOT starting with nl or fr here.
RewriteCond %{REQUEST_URI} !^/(nl|fr) [NC]
RewriteRule ^ /nl%{REQUEST_URI} [R=301,NE,L]

为了更好地理解您的问题,请您在问题中提及您希望从哪个URL重写/重定向到哪个URL(仅示例URL有效)。也分享你的htaccess文件,以便更好地理解,谢谢。谢谢你的回复,我已经更新了帖子。谢谢你的回复,它看起来很有效。使用wordpress多站点域映射仍然有困难,但它解决了我的问题。很高兴知道它成功了,@Andy,欢迎您,您也可以接受这个答案,单击我答案旁边的勾号,干杯。@Andy,很高兴这对您有效。你可以看到这个链接:干杯,快乐学习。