当遇到空格时,htaccess无法重定向

时间:2016-02-26 17:01:33

标签: apache .htaccess clean-urls

我正在使用htaccess来获取干净的网址。只要参数不携带空白,哪个工作正常。在这种情况下,它会转换%20中的空格,并收到404错误。

基本上是网址:http://localhost:8080/series/dynamics/admin/cleanURL/green%20apple

给我404错误。但网址:http://localhost:8080/series/dynamics/admin/cleanURL/greenapple效果很好。

还有一种方法可以从URL中删除目录详细信息,我试过

RewriteRule ^series/dynamics/admin/cleanURL/(.*)$ /$1 [L,NC,R]

但是没有工作

htaccess的

<IfModule mod_rewrite.c>
     Options +MultiViews 
     Rewriteengine on
     RewriteBase /series/dynamics/admin/cleanURL/
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^series/dynamics/admin/cleanURL/(.*)$ /$1 [L,NC,R]
     Rewriterule ^([a-zA-Z0-9]+)/?$ index.php?product=$1
     RewriteRule ^(.*)(\s|%20)(.*)$ /$3-$4 [R=301,L,NE]
     #rewrite group and subgroup e.g. http://.../value1/value2/ [L,NC,QSA]
     Rewriterule ^([a-zA-Z0-9]+(.*)+)/([^/]+(.*)+)/?$ index.php?product=$1&subgroup=$2 [L,NC,QSA]
     </IfModule>

1 个答案:

答案 0 :(得分:1)

您需要在站点根目录中添加此规则.htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) series/dynamics/admin/cleanURL/$1 [L]

然后在/series/dynamics/admin/cleanURL/.htaccess

中有这些规则
Options +MultiViews 
Rewriteengine on
RewriteBase /series/dynamics/admin/cleanURL/

RewriteCond %{THE_REQUEST} /series/dynamics/admin/cleanURL/(\S*)\s [NC]
RewriteRule ^ /%1 [L,R=302,NE]

RewriteRule ^([^\s\x20]*)[\s\x20]+(.*)$ $1-$2 [L,R=302,NE]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

Rewriterule ^(\w+)/?$ index.php?product=$1 [L,QSA]

#rewrite group and subgroup e.g. http://.../value1/value2/ [L,NC,QSA]
Rewriterule ^([a-zA-Z0-9]+(.*))/([^/]+(.*))/?$ index.php?product=$1&subgroup=$2 [L,NC,QSA]