简体   繁体   English

如何从目录及其所有子目录中删除所有.php扩展名?

[英]How to remove all .php extension from a dir and all of its subdirectories?

My web is dir structured is as follows : 我的网站目录结构如下:

moving/ 移动/
includes/ 包括/
controllers/ 控制器/
includes/ 包括/
etc... 等等...


I have the following questions about /moving and all of its sub-diretories: 关于/moving及其所有子目录,我有以下问题:

  1. How can i request any php script without the .php extension? 我如何请求没有.php扩展名的任何php脚本?
  2. How can i request ...dir/index.php?q=about as just ...dir/about ? 我怎样才能要求...dir/index.php?q=about只是...dir/about
  3. Where woul i place my .htaccess file? 我将.htaccess文件放在哪里?

I understand that my first question might be a duplicate, but in order to contextualize my second and third question i had to ask it. 我知道我的第一个问题可能是重复的,但是为了将第二个和第三个问题具体化,我不得不提出这个问题。

Using WampServer 2.0 Thanks in advance. 使用WampServer 2.0预先致谢。

apache will not parse your php script if they don't have the ".php" extention. 如果没有“ .php”扩展名,apache将不会解析您的php脚本。

but, you can use mod_rewrite : 但是,您可以使用mod_rewrite

This module uses a rule-based rewriting engine (based on a regular-expression parser) to rewrite requested URLs on the fly. 该模块使用基于规则的重写引擎(基于正则表达式解析器)来动态重写请求的URL。 It supports an unlimited number of rules and an unlimited number of attached rule conditions for each rule, to provide a really flexible and powerful URL manipulation mechanism. 它为每个规则支持无限数量的规则和无限数量的附加规则条件,以提供真正灵活而强大的URL操作机制。 The URL manipulations can depend on various tests, of server variables, environment variables, HTTP headers, or time stamps. URL操作可能取决于服务器变量,环境变量,HTTP标头或时间戳的各种测试。 Even external database lookups in various formats can be used to achieve highly granular URL matching. 甚至可以使用各种格式的外部数据库查找来实现高度粒度的URL匹配。

This module operates on the full URLs (including the path-info part) both in per-server context (httpd.conf) and per-directory context (.htaccess) and can generate query-string parts on result. 此模块在每个服务器上下文(httpd.conf)和每个目录上下文(.htaccess)中都对完整URL(包括path-info部分)进行操作,并且可以在结果上生成查询字符串部分。 The rewritten result can lead to internal sub-processing, external request redirection or even to an internal proxy throughput.internal proxy throughput. 重写的结果可能导致内部子处理,外部请求重定向,甚至导致内部代理吞吐量。内部代理吞吐量。

If all your website files are ending with .php and you want to take off the .php extension, then you should create and .htaccess file, or if you already have it, add this rule: 如果您所有的网站文件都以.php结尾,并且您要删除.php扩展名,则应创建.htaccess文件,或者如果已经拥有它,则添加以下规则:

RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L] RewriteRule ^(([[^ /] + /)* [^。] +)$ /$1.php [L]

Supposing your website pages are let's say site/about.php, site/contact.php, site/news.php, the rule above will just transform those to site/about, site/contact, site/news 假设您的网站页面是site / about.php,site / contact.php,site / news.php,则上述规则只会将其转换为site / about,site / contact,site / news

Hope it helps! 希望能帮助到你!

如果您将mod_rewrite路由非PHP扩展请求发送到正确的请求,则可以将其放在根Web目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM