PHP /的.​​htaccess:卸下php扩展的网址 [英] PHP/.htaccess: removing php extension from url

查看:212
本文介绍了PHP /的.​​htaccess:卸下php扩展的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的.htaccess code删除PHP扩展为我所有的网页。这里的code我用:

  RewriteEngine叙述上
的RewriteCond /%{REQUEST_FILENAME}.php -f
重写规则^([A-ZA-Z0-9 _- \ S] +)/ $ /$1.php
 

这似乎并没有工作。我想我失去了一些东西。当我输入www.mysite.com/about/得到www.mysite.com/about.php则返回错误404(找不到页面)。是否有人可以提供一些线索。

谢谢, 保罗-G。

解决方案

 选项+了FollowSymLinks -MultiViews

RewriteEngine叙述上
的RewriteBase /

#如果文件夹不存在
的RewriteCond%{} REQUEST_FILENAME!-d
#和文件存在
的RewriteCond%{DOCUMENT_ROOT} / $ 1 \ .PHP -f
#取消对下面的规则,如果你想在/被需要
#否则保持原样
#重写规则^([^ /] +)/ $ $ 1.PHP [L]
#内部显示filename.php的内容
重写规则^([^ /] +)/ $ $ 1.PHP [L]
 

以上规则将:

  1. 在不转接,如果一个文件夹中存在
  2. 如果该文件不存在
  3. 在不转接
  4. 将重定向什么来之前, / 如果是present作为文件名

因此​​,将适用于所有这些例子:

  http://domain.com/about/
http://domain.com/about
http://domain.com/contact/
http://domain.com/contact
 

如果你愿意,你可以删除,像评论规则,使其只接受URL的一个为此 /

  http://domain.com/about/
http://domain.com/contact/
 

现在这些都为上述工作的重要一步:

  1. 必须进入的.htaccess 。例如你的根文件夹 /home/youraccount/public_html/.htaccess
  2. Options(选项)重写规则之前,是非常重要的特殊 -MultiViews
  3. 在该文件必须存在于同一个地方的.htaccess 是例如在你的情况 about.php 文件
  4. 在PHP的必须工作效果显着。

I was using .htaccess code to remove .php extension for all my web pages. Here's the code I use:

RewriteEngine On
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php

It doesn't seem to work. I think I'm missing something. When I type www.mysite.com/about/ to get www.mysite.com/about.php it returns error 404 (page not found). Can someone please shed some light.

Thanks, Paul G.

解决方案

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# If folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# and file exist
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
# uncomment the below rule if you want the "/" to be required 
# otherwise leave as is
# RewriteRule ^([^/]+)/$ $1.php [L]
# internally show the content of filename.php
RewriteRule ^([^/]+)/?$ $1.php [L]

The above rule will:

  1. will not redirect if a folder exist
  2. will not redirect if the file does not exist
  3. will redirect what comes before the / if one is present as the file name

So it will work for all these examples:

http://domain.com/about/
http://domain.com/about
http://domain.com/contact/
http://domain.com/contact

If you want you can remove the ?, like the commented rule, to make it accept only URL's that end with a /.

http://domain.com/about/
http://domain.com/contact/

Now these are important step for the above to work:

  1. It must go into the .htaccess on your root folder for example /home/youraccount/public_html/.htaccess
  2. The Options before the rewrite rule are very important specially -MultiViews
  3. The file must exist on the same place the .htaccess is for example in your case the about.php file
  4. The PHP must be working obviously.

这篇关于PHP /的.​​htaccess:卸下php扩展的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆