Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/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从URL中删除方括号_.htaccess - Fatal编程技术网

.htaccess从URL中删除方括号

.htaccess从URL中删除方括号,.htaccess,.htaccess,我有许多带有方括号的URL。然后我更改了这些URL并删除了它们的所有方括号,但仍然会出现软404错误,因为旧URL在web中被索引。URL是不同的,我可以手动重定向每个URL,但是如果我在.htaccess中使用一些通用规则会更好,它会从URL中删除所有方括号 http://www.example.com/page-[first] http://www.example.com/page-[second] 等等。。将替换为: http://www.example.com/page-first h

我有许多带有方括号的URL。然后我更改了这些URL并删除了它们的所有方括号,但仍然会出现软404错误,因为旧URL在web中被索引。URL是不同的,我可以手动重定向每个URL,但是如果我在.htaccess中使用一些通用规则会更好,它会从URL中删除所有方括号

http://www.example.com/page-[first]
http://www.example.com/page-[second]
等等。。将替换为:

http://www.example.com/page-first
http://www.example.com/page-second
我可以用.htaccess吗

谢谢

试试这个:

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\[(.*)$ $1$2 [N,R=301]
RewriteRule ^(.*)\](.*)$ $1$2 [N,R=301]
如果.htacess位于子文件夹中,请使用以下命令更改
RewriteBase/

RewriteBase /sub-folder-name
这是我的
访问日志
——其中显示了重定向:

::1 - - [04/Jun/2016:12:29:23 +0800] "GET /test/hello-[world].html HTTP/1.1" 301 246
::1 - - [04/Jun/2016:12:29:23 +0800] "GET /test/hello-world.html HTTP/1.1" 200 3
::1 - - [04/Jun/2016:12:37:45 +0800] "GET /test/hello-%5bworld%5d.html HTTP/1.1" 301 246
::1 - - [04/Jun/2016:12:37:45 +0800] "GET /test/hello-world.html HTTP/1.1" 304 -
试试这个:

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\[(.*)$ $1$2 [N,R=301]
RewriteRule ^(.*)\](.*)$ $1$2 [N,R=301]
如果.htacess位于子文件夹中,请使用以下命令更改
RewriteBase/

RewriteBase /sub-folder-name
这是我的
访问日志
——其中显示了重定向:

::1 - - [04/Jun/2016:12:29:23 +0800] "GET /test/hello-[world].html HTTP/1.1" 301 246
::1 - - [04/Jun/2016:12:29:23 +0800] "GET /test/hello-world.html HTTP/1.1" 200 3
::1 - - [04/Jun/2016:12:37:45 +0800] "GET /test/hello-%5bworld%5d.html HTTP/1.1" 301 246
::1 - - [04/Jun/2016:12:37:45 +0800] "GET /test/hello-world.html HTTP/1.1" 304 -

您可以使用单个规则执行此重定向:

RewriteEngine on

RewriteCond %{THE_REQUEST} /page-(?:%5B|\[)(.*?)(?:%5D|\]) [NC]
RewriteRule ^ /%1 [R,L]

您可以使用单个规则执行此重定向:

RewriteEngine on

RewriteCond %{THE_REQUEST} /page-(?:%5B|\[)(.*?)(?:%5D|\]) [NC]
RewriteRule ^ /%1 [R,L]

詹姆斯,谢谢你的快速回复。我尝试过,但括号用%5b和%5d转换。在我这方面,即使URL是test-[page].html或test-%5bpage%5d.html,重定向也可以正常工作。您的.htaccess是否在子文件夹中?不,它在主/根文件夹中。请将此添加到下面的
RewriteEngine On
RewriteBase/
(请参阅上面编辑的答案)感谢您的快速回复。我尝试过,但括号用%5b和%5d转换。在我这方面,即使URL是test-[page].html或test-%5bpage%5d.html,重定向也可以正常工作。您的.htaccess是否在子文件夹中?不,它在主/根文件夹中。请将此添加到下面的
RewriteEngine On
RewriteBase/
(请参阅上面编辑的答案)