dtz55359 2017-09-29 23:48
浏览 68
已采纳

.htaccess - 如何从网址隐藏.php?

I'm looking for a way to clean up my site's urls, like removing .php or .html when there is a file name in the url. Also if it's possible to easily hide GET extensions that would be nice too, but the .php is the main goal.

From other articles and SO questions I have this code in the .htaccess file in the main directory of a website:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


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

The first part redirect to https, if someone types the url with http. That part work fine. The second 3 lines should use a url without the .php extension on files. For example https://www.fixitconnect.com/users.php should be https://fixitconnect.com/users

I don't know how this syntax works so i was just copying other answers. Looks like it sets some conditions (if there is .php in the current file's name), then a rule (use a url without the .php) to be applied if those conditions are met. But i'm probably not doing it right.

Thanks :)

  • 写回答

2条回答 默认 最新

  • dougu6815 2017-09-30 12:31
    关注

    Try the folowing code :

    DirectorySlash Off 
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.*)  /$1.html [L] 
    RewriteCond %{REQUEST_FILENAME}\.php -f 
    RewriteRule ^(.*)  /$1.php [L]
    RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(html|php)[\s?/] [NC] 
    RewriteRule ^(.*)  /%1%2 [R=302,L]
    RewriteCond %{REQUEST_URI} !(/$|\.) [NC]
    RewriteRule ^  %{REQUEST_URI}/ [R=302,L] 
    

    The above code will remove both html & php extensions then give priority to check that name without extensions to files first then folders .

    So , if you have file and folder with same name the priority will be for files when they comes without extensions .

    NOTE: if you have two files with same names and diffrent extentions the priority will be for .html and if you wanna to be for .php let fifth & sixth lines be in third & forth lines .

    The last line to slash directory again when they come without slash .

    If it is ok , change 302 to 301 to get permanent redirection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测