【问题标题】:htaccess rewrite if url extention is .php remove it如果 url 扩展名为 .php,则 htaccess 重写将其删除
【发布时间】:2013-11-05 02:44:51
【问题描述】:

我还想 301 重定向任何以 .php 结尾的网址。然后我在内部将任何以/ 结尾的内容重定向到/ 之前的名称加上.php。当他们独自一人时,它们都可以正常工作。但是将它们放在同一个 .htaccess 中,我得到一个 The page isn't redirecting properly

我在这里做错了什么?

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

RewriteRule ^(.*)/$ $1.php [QSA,L]

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    这些规则应该适合你:

    RewriteEngine On
    
    ## Adding a trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule . %{REQUEST_URI}/ [L,R=301]
    
    ## hide .php extension
    # To externally redirect /dir/file.php to /dir/file
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
    RewriteRule ^ /%1/ [R=301,L,NE]
    
    # To internally forward /dir/file/ to /dir/file.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.+?)/?$ /$1.php [L]
    

    【讨论】:

    • 这确实有效,谢谢。你知道如何强制尾部斜杠输入地址栏时它不存在吗?
    猜你喜欢
    • 2014-12-22
    • 2017-01-07
    • 1970-01-01
    • 2012-03-18
    • 2015-03-14
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    相关资源
    最近更新 更多