drfu80954 2013-04-03 16:40
浏览 26

从example.com/test.php/foo/bar中删除.php

I'm looking for a way to simply remove ".php" from a url while ignoring everything after the .php

So example.com/test.php/foo/bar would become example.com/test/foo/bar

and then in the PHP template I can search for and utilize foo or bar as I please. Everything I've found to remove .php interferes with the variables at the end.

UPDATE: I've found some success by removing .php from the file name and then using ForceType. Although It'd still be nice if I could keep the .php extension so my Code Editor knows how to highlight the syntax :)

<FilesMatch "^test$">
    ForceType application/x-httpd-php5
</FilesMatch>

Update 2 Here's a RewriteRule that I've tried using to some success, but when I add a trailing slash and some content after, it results in a 500 internal server error

RewriteEngine On
RewriteBase /
# remove .php; use THE_REQUEST to prevent infinite loops
    RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
    RewriteRule (.*)\.php$ $1 [R=301]
# remove index
    RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /$
    RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule (.*) $1\.php [L]
  • 写回答

2条回答 默认 最新

  • dongshi3361 2013-04-03 17:18
    关注

    Your last block is almost there. However you need a bigger assortment of RewriteConds to make it work with an optional PATH_INFO.

    Namely you need to match the word characters from the REQUEST_FILENAME/_URI first, and then test for existence of a like-named .php script:

    RewriteCond %{REQUEST_URI}           ^/(w+)/
    RewriteCond %{DOCUMENT_ROOT}/%1.php  -f
    RewriteRule ^(\w+)/(.+)$ $1.php/$2   [L]
    

    The last RewriteRule depends on existing trailing vars. Add one of the generic rule blocks for plain http://example.com/test requests without PATH_INFO. Note that this specific set will work with xyz.php scripts in the webroot only.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?