如何在RewriteRule中包含空格?

时间:2014-11-28 00:05:55

标签: .htaccess mod-rewrite url-rewriting

我需要更新我的.htaccess文件以进行URL重写。即使this post也无济于事。 我需要转移如下,但它不起作用!

第一项要求

(只是字母 - 不区分大小写 - 接受空格)

domain.com/Acco unting > domain.com/edit.jsp?kazem=Acco unting&oto=1&sab=
domain.com/acco unting > domain.com/edit.jsp?kazem=acco unting&oto=1&sab=
domain.com/Accounting > domain.com/edit.jsp?kazem=Accounting&oto=1&sab=
domain.com/accounting > domain.com/edit.jsp?kazem=accounting&oto=1&sab=

第二项要求

(第一部分只是字母表,接受空格 - 第二部分字母和数字,接受空格)

domain.com/Accounting/city 2222 > domain.com/edit.jsp?kazem=Accounting&oto=1&sab=city 2222
domain.com/Accou nting/city 2222 > domain.com/edit.jsp?kazem=Accou nting&oto=1&sab=city 2222

我的.htaccess如下

    <IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
</IfModule>
#Options +FollowSymLinks
#IndexIgnore */*

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !index
RewriteRule ^(.*)/(.*[0-9]+)$ /edit.jsp?kazem$1&oto=1&sab=$2 [L]
</ifModule>

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !index
#RewriteRule ^(.[a-zA-Z]+)$ /edit.jsp?kazem=$1&oto=1&sab= [L]
RewriteRule "^([a-z]+( [a-z]+)?)/?$" /edit.jsp?kazem=$1&oto=1&sab= [L]

</ifModule>

IndexIgnore *

<IfModule mod_headers.c>
  Header set Access-Control-Allow-Origin "*"
#  Header set Access-Control-Allow-Credentials true
  Header add Access-Control-Allow-Headers "*,origin, x-requested-with, content-type"
  Header add Access-Control-Allow-Methods "GET, POST, DELETE, OPTIONS"
</IfModule>


DirectoryIndex index.html index.php

2 个答案:

答案 0 :(得分:3)

此规则适用于您:

RewriteEngine On
RewriteBase /

RewriteRule "^([a-z]+( [a-z]+)?)/?$" edit.jsp?name=$1 [L,QSA,NC]
  • 请勿在目标URI中使用http://,否则它将成为R=302
  • 的外部重定向
  • 使用空格时引用模式

完成.htaccess:

IndexIgnore *    
DirectoryIndex index.html index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule ^([^/]+)/(\d+)/?$ edit.jsp?kazem$1&oto=1&sab=$2 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule "^(\S+(?: \S+)*)/?$" edit.jsp?kazem=$1&oto=1&sab= [L,QSA]

<IfModule mod_headers.c>
  Header set Access-Control-Allow-Origin "*"
  #  Header set Access-Control-Allow-Credentials true
  Header add Access-Control-Allow-Headers "*,origin, x-requested-with, content-type"
  Header add Access-Control-Allow-Methods "GET, POST, DELETE, OPTIONS"
</IfModule>

答案 1 :(得分:1)

您想使用NE RewriteRule flag

RewriteRule ^([a-z\s]+)$ http://example.com/edit.jsp?name=$1 [NC,NE,L]