使用 .htaccess 重写 URL 以隐藏 PHP 扩展名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18177290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:06:34  来源:igfitidea点击:

Rewrite URL with .htaccess to hide PHP extension

php.htaccessurlmod-rewriterewrite

提问by Jelmer Holtes

I've a question, I'm developing a webservice for a specific app. Last week I transferred my back-up from server1 to server2, since that moment my .htaccess won't work anymore.

我有一个问题,我正在为特定应用程序开发网络服务。上周我将备份从 server1 转移到 server2,因为那一刻我的 .htaccess 将不再工作。

The url for my webservice is: http://apps.holtesdesign.nl/inholland/apiv0/

我的网络服务的网址是:http: //apps.holtesdesign.nl/inholland/apiv0/

This url works fine, to load the bugreport module: http://apps.holtesdesign.nl/inholland/apiv0/bugreport.php

这个网址工作正常,加载错误报告模块:http: //apps.holtesdesign.nl/inholland/apiv0/bugreport.php

But the url was supposed to be: http://apps.holtesdesign.nl/inholland/apiv0/bugreport

但网址应该是:http: //apps.holtesdesign.nl/inholland/apiv0/bugreport

When you'll open the last url, you'll see that it will result in an internal server error. Do you know how it's possible?

当您打开最后一个 url 时,您会看到它会导致内部服务器错误。你知道怎么可能吗?

My .htaccess looks like:

我的 .htaccess 看起来像:

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

The server is running on CentOS, and I've already checked if mod_rewrite is functioning...

服务器在 CentOS 上运行,我已经检查过 mod_rewrite 是否正在运行......

I hope you can help me guys!

我希望你们能帮助我!

Cheers,

干杯,

Jelmer

杰尔默

回答by Jacob Pollack

This will fix it:

这将修复它:

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


If it doesn't work correct the settings of your Wamp Server:

如果它不起作用,请更正 Wamp 服务器的设置:

  1. Left click WAMP icon
  2. Apache
  3. Apache Modules
  4. Left click rewrite_module.
  1. 左键单击 WAMP 图标
  2. 阿帕奇
  3. Apache 模块
  4. 左键单击 rewrite_module。

回答by shaz3e

This will hide the trailing slash and hide .php in root & sub directories.

这将隐藏尾部斜杠并隐藏根目录和子目录中的 .php。

RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ .php [NC,L]

回答by Lakshman Kambam

try this.. it works! :)

试试这个..它有效!:)

Options +FollowSymLinks
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ .php

回答by Irshad Khan

Working Concept for me:

我的工作理念:

# Options is required by Many HostSevice
Options +MultiViews

RewriteEngine on

# For .php & .html URL's:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ .php [NC,L]
RewriteRule ^([^\.]+)$ .html [NC,L]

Use this code in Root folder of your website in .htaccess File : Example

在 .htaccess 文件中的网站根文件夹中使用此代码:示例

offline - wamp\www\YourWebDir

离线 - wamp\www\YourWebDir

online - public_html/

在线 - public_html/

Important Note : If it doesn't work correct the settings of your Wamp Server: 1) Left click WAMP icon 2) Apache 3) Apache Modules 4) Left click rewrite_module

重要说明:如果它不起作用,请更正 Wamp 服务器的设置:1) 左键单击 WAMP 图标 2) Apache 3) Apache 模块 4) 左键单击 rewrite_module