【问题标题】:Removing file extension apache server删除文件扩展名 apache 服务器
【发布时间】:2012-03-18 20:59:57
【问题描述】:

今天我想实现这一点,其中 .htaccess 会自动从浏览器的 url 中删除任何类型的文件扩展名。 例如,用户单击链接以将用户带到
mylink.php
它应该把他带到那里,但删除 .php



我尝试使用一些 .htaccess 代码,但它们并没有做太多。

我也可以像这样访问页面

  • localhost/register
  • localhost/register/

    .htaccess 代码是

        RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^([^/]+)/$ $1.html 
    
    # Forces a trailing slash to be added
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)$ /$1/ [R=301,L]
    

    大家有什么想法吗?

  • 【问题讨论】:

    • 什么 .htaccess你尝试过什么?显示您拥有的实际代码。
    • 我只是希望它自己删除文件扩展名...
    • @Marshall Mathews:所以你不担心失去风格?如果不是,请从您的问题中删除该部分。

    标签: php apache .htaccess web


    【解决方案1】:
    RewriteEngine On
    
    # Change this to the appropriate base (probably just / in your case)
    RewriteBase /stackoverflow/9762238/
    
    # Any url that requests a .php file on the base will be redirected to dir with same base name 
    # The condition is important to prevent redirect loops!
    RewriteCond %{ENV:REDIRECT_STATUS} !=200
    RewriteRule ^(.*)\.php$ $1/ [R=301,L]
    
    # Requests for directories that do not actually exist will be be translated into php file requests
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule (.+)/$ $1 [L] 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      相关资源
      最近更新 更多