如何在URL末尾添加尾部斜杠

时间:2018-02-06 07:46:43

标签: php .htaccess redirect mod-rewrite

这是我的.htaccess文件:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule !\.(css|js|ico|zip|rar|png|jpg|gif|pdf)$ index.php [L]
AddType application/x-httpd-php .php .phtml

我需要知道当我有这个代码时如何在URL的末尾添加尾部斜杠:

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

www中添加.htaccess 例如,没有代码的URL:

  

www.example.com/introduction

并使用此代码:

  

www.example.com/introduction /

2 个答案:

答案 0 :(得分:0)

这样做:

AddType application/x-httpd-php .php .phtml
RewriteEngine On
RewriteBase /

# add www
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# add a trailing slash to non-files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(css|js|ico|zip|rar|png|jpe?g|gif|pdf)$ index.php [L,NC]

答案 1 :(得分:0)

IIS用户规则:添加/到所有URL。适用于所有seo

   <rule name="Add trailing slash" stopProcessing="true">
      <match url="^(.*)([^/])$" />
      <conditions logicalGrouping="MatchAny">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" pattern="(.*?)\.[a-zA-Z]{1,4}$" negate="true" />
      </conditions>
      <action type="Redirect" url="{R:0}/" redirectType="Permanent" />
    </rule>