htaccess的重定向所有扩展到PHP [英] .htaccess redirect all extension to php

查看:123
本文介绍了htaccess的重定向所有扩展到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近更新了我的的.htaccess 文件以隐藏的URL的 PHP扩展的。它的工作原理很好,当我试图达到的 domain.com/index 的,它显示的PHP文件。

当我输入的 domain.com/index.php 的重定向我到的 domain.com/index 的URL,并显示PHP文件也是如此。

我想所有其他扩展重定向到没有扩展名的URL来显示出于安全原因的PHP文件。我的问题是,我无法配置我的的.htaccess 重定向众所周知扩展喜欢的的.html .asp的。的aspx的.shtml 的等,以非扩展网址,并获得显示为内容的PHP文件

我的.htaccess文件看起来是这样的:

 < IfModule mod_rewrite.c>
RewriteEngine叙述上
的RewriteBase /

#PHP扩展没有扩展名的URL
的RewriteCond%{THE_REQUEST} ^ [AZ] + \ S + \ PHP \ SHTTP /.+
重写规则^(。+)\。PHP $ 1 [R = 301,L]


#没有扩展到PHP文件
的RewriteCond%{} REQUEST_FILENAME -f .PHP
的RewriteCond%{REQUEST_URI}!/ $
重写规则(。*)$ 1 \ .PHP [L]

#这是该指数
重写规则^指数\ .PHP $  -  [L]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则。的index.php [L]
< / IfModule>
 

解决方案

您可以尝试这样的:

 #PHP扩展没有扩展名的URL
的RewriteCond%{THE_REQUEST} ^ [AZ] + \ S + \ PHP \ SHTTP /.+
重写规则^(。+)\。[A-ZA-Z0-9] + $ $ 1 [R = 301,L]
 

然后,它不仅会重定向的.php 扩展,但任何人。要使用它,请确保你没有URL以一个点和字母数字字符串结尾。

I recently updated my .htaccess file to hide the .php extension of the urls. It works nice, when I try to reach domain.com/index, it displays the php file.

When I type domain.com/index.php it redirects me to the domain.com/index url and displays the .php file as well.

I would like to redirect all other extensions to the no extension url to display the .php file for security reasons. My problem is, that I can not configure my .htaccess to redirect well known extensions like .html .asp. aspx .shtml etc. to the non-extension url, and get the .php file displayed as the content.

My .htaccess file looks like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# php extension to no extension url
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]


# no extension to the .php file
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

# this is for the index
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

解决方案

You could try something like this:

# php extension to no extension url
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.[a-zA-Z0-9]+$ $1 [R=301,L]

Then it will not only redirect .php extension, but anyone. To use it, be sure that you dont have URL with a dot and an alphanumeric string at the end.

这篇关于htaccess的重定向所有扩展到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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