0

我的网址看起来像

 localhost/test/page1.php

我想隐藏 php 扩展所以我使用下面的方法来隐藏 php 扩展

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

现在网址是

localhost/test/page1

但是当我使用 localhost/test/page1/ 之类的 url 时,它会显示错误。如何将斜杠 url 重定向到非斜杠 url

4

2 回答 2

1

测试这个:

RewriteRule ^([^/]+)/?$ $1.php [NC,L]
于 2013-07-15T23:07:09.530 回答
0

只需添加一个重写规则,如下所示:

RewriteEngine on

# Remove slashes from incoming URI's that end with slashes.
RewriteRule ^/?(.+)/$ /$1 [R=301,L]

# Redirect redirect requests to PHP controllers
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^\.]+)$ /$1.php [NC,L]

这会将任何请求重定向到不以斜杠结尾的 Web 根目录,并带有 HTTP 301 标头的斜杠清理 URL。

于 2013-07-15T23:00:45.547 回答