Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.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 阻止访问seo不友好的URL并重定向到seo友好(规范)URL_.htaccess_Mod Rewrite_Url Redirection_Friendly Url - Fatal编程技术网

.htaccess 阻止访问seo不友好的URL并重定向到seo友好(规范)URL

.htaccess 阻止访问seo不友好的URL并重定向到seo友好(规范)URL,.htaccess,mod-rewrite,url-redirection,friendly-url,.htaccess,Mod Rewrite,Url Redirection,Friendly Url,我看到许多类似的问题,但迄今为止没有一个对我有帮助: 我的脚本逻辑在/song/song.php?url=lang/params(seo不友好的url)下。我使用这个重写来创建一个友好的URL,并从seo非友好URL加载内容,使用一个参数来区分不同的语言: RewriteRule ^songs-and-chords/(.*?)(/*)?$ /song/song.php?url=en/$1 [NC,L] RewriteRule ^canzoni-e-accordi/(.*?)(/*)?$ /s

我看到许多类似的问题,但迄今为止没有一个对我有帮助:

我的脚本逻辑在/song/song.php?url=lang/params(seo不友好的url)下。我使用这个重写来创建一个友好的URL,并从seo非友好URL加载内容,使用一个参数来区分不同的语言:

 RewriteRule ^songs-and-chords/(.*?)(/*)?$ /song/song.php?url=en/$1 [NC,L]
 RewriteRule ^canzoni-e-accordi/(.*?)(/*)?$ /song/song.php?url=it/$1 [NC,L]
现在我想阻止对/song/song.php的访问,强制重定向到seo友好型,所以类似这样,使用%{the_REQUEST}进行外部重定向:

 RewriteCond %{THE_REQUEST} .*?song/song\.php\?url=it/(\S*) [NC]
 RewriteRule /canzoni-e-accordi/$1 [R,L]
 RewriteCond %{THE_REQUEST} .*?song/song\.php\?url=en/(\S*) [NC]
 RewriteRule /songs-and-chords/$1 [R,L]
 RewriteCond %{THE_REQUEST} ^.*?song/song\.php.*$ [NC]
 RewriteRule /songs-and-chords/ [R,L]
但这条规则的任何一条都被执行了。。。我做错了什么?我怎样才能以一种好的方式调试它


(我已经在song.php脚本中使用了rel=“canonical”,但我还是希望该脚本完全不可访问)。

你做错了的是,你的
RewriteRule
语句的模式丢失了。此外,重写的URL将使用
%1
引用,而不是
$1

RewriteCond %{THE_REQUEST} ^GET\ /song/song\.php\?url=it/(\S+) [NC]
RewriteRule ^ /canzoni-e-accordi/%1? [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /song/song\.php\?url=en/(\S+) [NC]
RewriteRule ^ /songs-and-chords/%1? [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /song/song\.php\s [NC]
RewriteRule ^ /songs-and-chords/ [R=301,L]

谢谢,没错。唯一的问题是:这个URL/song/song.php?URL=it/pink-floyd/墙重定向到/canzoni-e-accordi/pink-floyd/墙?URL=it/pink-floyd/墙如何摆脱附加的URL参数?QSA标志没有设置,那么它来自哪里?(获取后的“\”是什么?@Fabbio将
%1
更改为
%1?
将修复重定向url中添加的查询字符串。至于
GET\
string;发送到服务器的请求的格式为:
GET/song/song.php?url=it/pink floyd/the wall HTTP/1.1
。我在
GET
之后逃离空间。您也可以使用
\s
而不是
\
像一个符咒一样工作!但是我现在有点迷路了。。。%1是什么意思?这不是regex的东西,对吧?我也不清楚为什么我应该以“^”开始重写规则。关于这些问题,你能推荐一些好的文档页面吗?@Fabbio<代码>%1表示来自
RewriteCond
语句的匹配组,
$1
表示来自
RewriteRule
s的匹配组。在rewriterule中使用
^
时,我完全忽略了url(在处理原始请求时,无论如何都不需要url)。