【问题标题】:Allow ' and . through to URL on .htaccess允许 ' 和 .通过 .htaccess 上的 URL
【发布时间】:2014-03-25 15:56:09
【问题描述】:

我有一个 RewriteRule 可以在我的网站上重命名我的 URL,允许字母和空格通过。我想知道如何更改我的正则表达式以在 URL 中也允许单引号和句点。这是我的 .htaccess:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^(\w+)$ ./index.php?role=$1
RewriteRule ^(\w+)/$ ./index.php?role=$1
RewriteRule ^([\w]+)/([\w\s]+)&([\s\w]+)$ ./result.php?role=$1&champ1=$2&champ2=$3

【问题讨论】:

  • 你逃脱他们,就像你的第 8 行:\.\'
  • @Martijn 那么底线看起来像:RewriteRule ^([\w]+)/([\w\s\.\']+)&([\s\w\'\.]+)$ ./result.php?role=$1&champ1=$2&champ2=$3 吗?
  • @user1895377:应该可以。如果不提供您要匹配的示例 URI。

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

你避开它们,就像你的第 8 行:\.\'

此外,如果添加问号,则可以删除第 9 行:

# These two:
RewriteRule ^(\w+)$ ./index.php?role=$1
RewriteRule ^(\w+)/$ ./index.php?role=$1
# Can be replaced by:
RewriteRule ^(\w+)/?$ ./index.php?role=$1

# And if I understand it properly, the last two lines don't happen both:
# You can replace the bottom 3 lines with this:
RewriteRule ^(\w+)/?$ ./index.php?role=$1 [L]
RewriteRule ^([\w]+)/([\w\s]+)&([\s\w]+)$ ./result.php?role=$1&champ1=$2&champ2=$3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 2013-08-10
    • 2011-05-31
    • 2016-12-22
    • 2017-08-15
    • 1970-01-01
    相关资源
    最近更新 更多