【问题标题】:How can I use .htaccess to hide .php URL extensions?如何使用 .htaccess 隐藏 .php URL 扩展名?
【发布时间】:2012-04-19 03:48:55
【问题描述】:

我想在我的 .htaccess 文件中添加一些内容,以隐藏我的 php 文件的 .php 文件扩展名,因此访问 www.example.com/dir/somepage 会显示 www.example.com/dir /somepage.php.

有什么可行的解决方案吗?我的网站使用 HTTPS,如果这很重要的话。

这是我目前的 .htaccess:

RewriteEngine On

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

ErrorDocument 404 /error/404.php

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    在 DOCUMENT_ROOT 下的 .htaccess 中使用此代码:

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1 [R=302,L,NE]
    
    ## To internally redirect /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME}.php -f [NC]
    RewriteRule ^ %{REQUEST_URI}.php [L]
    

    需要注意的是,这也会影响所有 HTTP 请求,包括 POST,随后会影响所有此类请求,使其属于此重定向,并可能导致此类请求停止工作。

    要解决此问题,您可以在第一个 RewriteRule 中添加一个异常以忽略 POST 请求,因此不允许它们使用该规则。

    # To externally redirect /dir/foo.php to /dir/foo excluding POST requests
    RewriteCond %{REQUEST_METHOD} !POST
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1 [R=302,L,NE]
    

    【讨论】:

    • 如何排除页面?这似乎干扰了我的登录脚本,所以我想从这些重写中排除该页面
    • 谢谢会试一试。我认为正在发生的是,当页面在没有扩展名的情况下重新加载时,它会丢失变量。有没有办法来解决这个问题?好像它登录了两次......我想......一次通过电子邮件并通过,然后再次没有。所以我无法登录
    • @Arios:在第一个重定向规则中添加 RewriteCond %{REQUEST_METHOD} !POST 条件以避免 POST 请求被重定向。
    • @anubhava 你救了我的命
    • @anubhava 3年后,你也救了我的命
    【解决方案2】:

    移除 php 扩展

    您可以使用 /.htaccess 中的代码:

    RewriteEngine on
    
    
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^ %{REQUEST_FILENAME}.php [NC,L]
    

    使用上面的代码,您将能够访问 /file.php 作为 /file

    删除 html 扩展名

    RewriteEngine on
    
    
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^ %{REQUEST_FILENAME}.html [NC,L]
    

    注意:'RewriteEngine on' 指令应该在每个 htaccess 的顶部使用一次,所以如果你想组合这 2 个规则,只需从第二个规则中删除该行。您还可以删除您选择的任何其他扩展名,只需在代码中将 .php 替换为它们即可。

    访问愉快!

    【讨论】:

      【解决方案3】:
      <IfModule mod_rewrite.c>
      RewriteEngine on
      
      RewriteCond %{REQUEST_FILENAME} !-f       # if the requested URL is not a file that exists
      RewriteCond %{REQUEST_FILENAME} !-d       # and it isn't a directory that exists either
      RewriteCond %{REQUEST_FILENAME}\.php  -f  # but when you put ".php" on the end it is a file that exists
      RewriteRule ^(.+)$ $1\.php [QSA]          # then serve that file (maintaining the query string)
      
      </IfModule>
      

      奇怪的是,当我在 Windows 的 XAMPP 中尝试时,这段代码给了我 HTTP 500 错误。删除 cmets 修复了它。移动 cmets 也是如此,因此它们在自己的线上,而不是与指令在同一线上。

      【讨论】:

      • @Markum。从那以后,我发现某些实现不喜欢与代码在同一行上的 cmets。尝试删除 cmets。
      • 有同样的问题 - 删除 cmets 解决了它。
      【解决方案4】:

      试试这个:

      # Unless directory, remove trailing slash
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]
      
      # Redirect external .php requests to extensionless url
      RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
      RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
      
      # Resolve .php file for extensionless php urls
      RewriteRule ^([^/.]+)$ $1.php [L]Try:
      

      参考:

      How to hide .php extension in .htaccess

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-13
        相关资源
        最近更新 更多