删除扩展mod_rewrite太宽容了

时间:2017-01-05 03:52:36

标签: php apache .htaccess mod-rewrite

我正在尝试将example.com/news.php重写为example.com/newsexample.com/news/。我的代码example.com/news工作得很好但example.com/news/给了我 404 Not Found

我的代码:

<Directory /var/www/>
    Options -Indexes SymLinksIfOwnerMatch -MultiViews
    AllowOverride None
    Order allow,deny
    allow from all

    <IfModule mod_rewrite.c>

        RewriteEngine On

        RewriteBase /

        RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
        RewriteRule ^ %1 [R=301,L]

        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME}.php -f
        RewriteRule ^([^\.]+)$ $1.php [NC,L]

        RewriteRule ^games/csgo/([0-9]+)$ /games/csgo/view.php?id=$1 [L]

    </IfModule>
</Directory>

如何将example.com/news.php重定向到example.com/newsexample.com/news/

1 个答案:

答案 0 :(得分:2)

你应该更好地坚持一个url类型! (带或不带斜杠)并将错误的类型重定向到另一个,

此代码用于从url的末尾删除斜杠:

 RewriteRule ^(.*)/$ /$1 [L,R=301]

如果您之前添加RewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME}.php -f,则它也适用于子目录。