删除htaccess中目录和url查询之间的尾部斜杠

时间:2017-01-14 06:05:44

标签: apache .htaccess

我用PHP制作博客。我希望人们能够使用url查询加载页面,例如:

  

blog.mysite.com/article?a=my_cool_article

我实际上没有"文章"目录,我只有一个名为article.php

的文件

我在.htaccess中使用以下内容重定向请求:

DirectorySlash Off
RewriteEngine on
RewriteRule ^article?([a-zA-Z0-9\-_]+)/?$ /article\.php?article=$1 [QSA,L]

它运行正常,但是当我尝试使用它时,它会在article?之间添加斜杠。它会将网址更改为:

  

blog.mysite.com/article/?a=my_cool_article

我在这里尝试过一堆解决方案,却无法找到有效的方法。感谢您花时间阅读!

以下是我现有的规则:

DirectorySlash Off
RewriteEngine on
RewriteRule ^article?([a-zA-Z0-9\-_]+)/?$ /article\.php?article=$1 [QSA,L]

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]

Options +FollowSymLinks

RewriteOptions inherit
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Origin: "https://www.example.com"
Header set Access-Control-Allow-Origin: "https://example.com"
Header set Access-Control-Allow-Origin: "https://*.example.com"
Header set Access-Control-Allow-Origin: "https://src.example.com"
Header set Access-Control-Allow-Origin: "https://example.com/*"
Header set Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
Header set Strict-Transport-Security "max-age=31536000" env=HTTPS

AddDefaultCharset utf-8

<Files error_log>
    Order allow,deny
    Deny from all
    Satisfy All
</Files>

1 个答案:

答案 0 :(得分:1)

要删除尾部斜杠,可以使用:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]