dppi5167 2014-09-29 09:50
浏览 25
已采纳

从url中删除.php [重复]

This question already has an answer here:

Well, I know that it should be done by .htaccess my .htaccess is in the root of my site and here it's content

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Also mod rewrite is enabled at my server. I have a form with an action on ./getReadings.php in it. But when I change it to ./getReadings it says that tehre isn't such file on server. What's my mistake?

</div>
  • 写回答

3条回答 默认 最新

  • dongzhuo3202 2014-09-29 09:55
    关注

    Try adding Options -MultiViews before RewriteEngine directive

    Options -MultiViews
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ /$1.php [L,QSA]
    

    Note: MultiViews is about Apache content negociation (which is the problem here since filename will automatically be translated as filename.php existing file). That's why you have to disable it.


    EDIT: right now, you can access same content by 2 different urls (with or without php extension). To avoid duplicate content (which is bad for search engines) you can redirect extensions to extensionless equivalent with this code

    Options -MultiViews
    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} \s/(.+?)\.php [NC]
    RewriteRule . /%1 [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ /$1.php [L,QSA]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)
  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥15 隐藏系统界面pdf的打印、下载按钮