从URL中删除.php

从URL中删除.php,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,Ubuntu 14.04LTS 32位 灯 我知道这是个老问题,但是 我需要它将.php从可见url中的任何位置删除。 它需要使用/showthread.php?id=XX-->/showthread?id=XX 我甚至不能让它与/page.php-->/page一起工作。 我试过这些: 它什么都不做。 而其他.htaccess代码工作正常 当 # # 即使这样也没有任何影响: RewriteRule ^page$ page.php [L] sudo服务apache2重启不会改变任何东

Ubuntu 14.04LTS 32位

我知道这是个老问题,但是

我需要它将.php从可见url中的任何位置删除。 它需要使用/showthread.php?id=XX-->/showthread?id=XX

我甚至不能让它与
/page.php
-->
/page
一起工作。 我试过这些:

它什么都不做。 而其他
.htaccess
代码工作正常

#

#

即使这样也没有任何影响:

RewriteRule ^page$ page.php [L]
sudo服务apache2重启
不会改变任何东西

服务器重新启动不会改变任何内容

我试图清除里面的其他代码,没有做任何更改

我清除了浏览器缓存100次


我开始觉得它讨厌我。可能是什么原因造成的???

可能是重复的,但看看这个:


在解决方案中,只需将
html
更改为
php

就可以了。但是,听起来好像没有执行.htaccess文件。仅仅因为模块被激活,并不意味着它在您的特定情况下对您可用

以下是解决问题的一些步骤:

  • 首先,仔细检查拼写。验证拼写是否正确(包括开头的.)
  • 检查文件权限。某些服务器将需要可执行权限。所以chmod文件到755
  • 如果您仍然无法使用它,请进入您的apache配置(可能位于Ubuntu上的/etc/apache2/apache2.conf),找到AllowOverride的每个实例。它可能被设置为“无”。改为
    AllowOverride all
  • 然后进入启用的站点,找到您的站点配置,并更改其中的AllowOverride字段
  • 重新启动Apache服务器,并用一大杯咖啡祝贺自己
其中一个应该可以解决它。我建议您在每个步骤之间进行尝试,这样您就可以确定错误发生的位置。在确定原因后,您可能希望返回并根据您的需要限制某些允许的过度驾驶


祝你好运

您可以尝试以下方法

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php$2 [L]
这对我很有用:

    #On rewrite
    RewriteEngine On

    # Allow any files or directories that exist to be displayed directly
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d

    # rewrite domain.com/username.php to username
    RewriteRule ^([A-Za-z0-9_-]+)/?$ $1.php [NC,L]
    RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/?$ $1/$2.php [NC,L]
    RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ $1/$2/$3.php [NC,L]
还有更多: 查看我为社交门户网站(如fb)编写的重写文件:

# allow only from ip range /8 /16 /24 /31
    #Order deny,allow
    #Deny from all
    #Allow from 89.230.0.0/16

    # On rewrite
    RewriteEngine On

    # [NC]- case-insensitive 
    # [L] - Last Rule , stop the rewriting process here 
    # [OR] = Or - If it matches this condition or the next 
    # [QSA] -   Append query string to rewriting url
    # [R] - redirect [R=301] move permanently, 302 - temporarily, 403 - Forbidden, 404 - Not Found,  410 - Gone

    # fix folder redirect images and css js/images/css/media
    RewriteRule ^.+?/((img|css|js|media|upload|posts)/.+)$ /$1 [L,R=301,NC]

    # Allow any files or directories that exist to be displayed directly
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d
    # does not work on IIS windows server url rewrite when importing
    # RewriteCond %[REQUEST_FILENAME] !-l

    # rewrite example.xx/index.php na example.xx/
    RewriteCond %{THE_REQUEST} ^.*/index\.php 
    RewriteRule ^(.*)index\.(php|html?)$ / [R=301,NC,L]


    #portfolio rewrite folder rewrite
    # RewriteRule ^([portfolio]+)/?$ /portfolio/index.php?id=$1 [NC,L]

    # pretty url example.xx/id/post/number 
    # rewrite domain.com/username like twitter or facebook users
    RewriteRule ^([A-za-z0-9_-]+)/?$ index.php?username=$1 [NC,L]
    # domain.com/post/name
    RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/?$ profil.php?id=$1&menu=$2 [NC,L]
    # domain.com/cat/post/id
    RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/([0-9]+)/?$ profil.php?id=$1&menu=$2&page=$3 [NC,L]

    #RewriteRule ^([A-Za-z0-9_-]+)/(ustawienia)/?$ ustawienia.php?id=$1&menu=$2 [NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/(wpisy)/?$ profil0.php?id=$1&menu=$2 [QSA,NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/(fani)/?$ profil1.php?id=$1&menu=$2 [QSA,NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/(ogladasz)/?$ profil2.php?id=$1&menu=$2 [QSA,NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/(zdjecia)/?$ profil3.php?id=$1&menu=$2 [QSA,NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/(video)/?$ profil4.php?id=$1&menu=$2 [QSA,NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/(ulubione)/?$ profil5.php?id=$1&menu=$2 [QSA,NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/(wiadomosci)/?$ profil6.php?id=$1&menu=$2 [QSA,NC,L]
    #RewriteRule ^([A-za-z0-9_-]+)/?$ profil.php?id=$1 [NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/?$ profil.php?id=$1&menu=$2 [NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ index.php?id=$1&dir=$2&post=$3 [NC,L]

    # Redirect all subdomains
    # RewriteCond %{HTTP_HOST} ^(.*)\.breakermind\.com
    # RewriteRule ^(.*)$ http://breakermind.com/$1 [R=301,QSA,NC,L]

    # Redirect all subdomains
    # RewriteCond %{HTTP_HOST} ^www.ns2.breakermind.com [NC]
    # RewriteRule ^(.*)$ http://breakermind.com/$1 [R=301,QSA,NC,L]

    # RewriteCond %{HTTP_HOST} ^ns2.breakermind.com [NC]
    # RewriteRule ^(.*)$ http://breakermind.com/$1 [R=301,QSA,NC,L]

    # Redirect from www. to non-www (unhash if need)
    # RewriteCond %{HTTP_HOST} ^www.breakermind.com [NC]
    # RewriteRule ^(.*)$ http://breakermind.com/$1 [R=301,QSA,NC,L]

    # Redirect from http:// to https:// (from no-ssl to ssl)
    #RewriteCond %{SERVER_PORT} 80
    #RewriteRule ^(.*)$ https://breakermind.com/$1 [R=301,QSA,NC,L]


    ErrorDocument 400 /er404.html
    ErrorDocument 401 /er404.html
    ErrorDocument 402 /er404.html
    ErrorDocument 403 /er403.html
    ErrorDocument 404 /er404.html
    ErrorDocument 500 /er404.html
    ErrorDocument 502 /er404.html
    ErrorDocument 504 /er404.html

    #RewriteEngin On
    #RewriteCond %[REQUEST_FILENAME] !-d
    #RewriteCond %[REQUEST_FILENAME] !-f
    #RewriteCond %[REQUEST_FILENAME] !-l
    #RewriteRule ^(.+)$ index.php?c=$1 [QSA,L]
    #
    #        \w = [A-Za-z0-9_]  \d = [0-9]
以下是我的博客系统:

        Options +FollowSymLinks
    RewriteEngine On

    RewriteCond %{THE_REQUEST} ^.*/index\.php 
    RewriteRule ^(.*)index\.(php|html?)$ / [R=301,NC,L]

    # category
    RewriteRule   ^category/?$ index.php?id=0&page=0  [NC,L]
    RewriteRule   ^category/([A-Za-z0-9]+)/?$ index.php?id=$1&page=0  [NC,L]
    RewriteRule   ^category/([A-Za-z0-9]+)/([0-9]+)/?$ index.php?id=$1&page=$2  [NC,L]
    RewriteRule   ^category/([A-Za-z0-9]+)/([0-9]+)/([A-Za-z0-9]+)/?$ index.php?id=$1&page=$2&title=$3  [NC,L]

    # autor
    RewriteRule   ^autor/?$ index.php?id=0&page=0  [NC,L]
    RewriteRule   ^autor/([A-Za-z0-9]+)/?$ index.php?id=$1&page=0  [NC,L]
    RewriteRule   ^autor/([A-Za-z0-9]+)/([0-9]+)/?$ index.php?id=$1&page=$2  [NC,L]

    # article, gallery, tags
    RewriteRule   ^article/([A-Za-z0-9]+)/?$ article.php?id=$1    [NC,L]
    RewriteRule   ^gallery/([A-Za-z0-9]+)/?$ gallery.php?id=$1    [NC,L]
    RewriteRule   ^tags/([A-Za-z0-9]+)/?$ tags.php?id=$1    [NC,L]
    RewriteRule   ^archive/([0-9]+)/([0-9]+)/?$ archive.php?year=$1&month=$2    [NC,L]

    # custom page rewrite
    RewriteRule   ^page/([A-Za-z0-9]+)/?$ page.php?id=$1    [NC,L]

    # fix folder redirect images and css js/images/css/media
    RewriteRule ^.+?/((admin|img|css|js|media|upload|posts)/.+)$ /blog/$1 [L,R=301,NC]

    # user or category user.php
    # RewriteRule   ^([A-Za-z0-9]+)/?$ index.php?user=$1  [NC,L]

    # example
    # RewriteRule ^([folder])/([category])/([A-Za-z0-9]+)/?$ index.php?id=$3&cat=$2 [NC,L]

假设正在处理.htaccess,那么这个超级简单的.htaccess应该适用于您:

RewriteEngine on
RewriteRule (.*) $1.php [END]
如果它不工作,则表明您的Apache配置有问题。 你需要先调查一下

如果有效,请在重写规则之前添加以下行以允许服务其他文件:

RewriteCond %{REQUEST_FILENAME} !-f

结束标志自Apache 2.3.9起可用。

尝试从文件中完全删除.php扩展名并避免无限循环:

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
此代码将在Root/.htaccess中工作,如果要将其放置到子目录中的htaccess文件中,请确保更改RewriteBase。

检查

这是使用.htaccess的答案:

RewriteEngine On

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
使用WAMP在Windows上测试并正常工作。

希望有所帮助

这对我有用

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# For LocalHost !.php
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=::1

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
在我的Apache配置中启用该功能为我带来了好处:

内容协商,或者更准确地说,内容选择,是从多个可用文档中选择最符合客户能力的文档


它正在工作,你可以试试这个代码

1。首先在主目录中创建
.htaccess
文件,并粘贴相同的代码,然后清除历史记录及其工作状态。

RewriteEngine on

RewriteCond $1 !^(index\.php|resources|robots\.txt)

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L,QSA] 


这可能是你想要匹配的。您是否试图在浏览器
showthread中使用此选项?id=XX
RewriteRule无法匹配查询字符串。所以这可能就是它不起作用的原因。你的主要目标是什么?你看到这个问题了吗?答案对我也没有影响。我只是在检查example.com/page(返回404)和example.com/page.php(displays.php但可以工作)。我已经很高兴了,如果这样的话..我想我将永远使用.php:/@anubhava它位于php文件所在的子文件夹中。无论如何,我现在发现拉奥·阿西夫·拉扎的解决方案是有效的。谢谢你这个解决方案对我没有效果,你能提供一些解释吗?像黑摩西州,你能提供一些额外的解释,关于使用什么?我试过第一部分,但恐怕不行。在使用“.php”扩展名#而不使用“.php”扩展名重写规则^(.*)$$1.php访问页面时,我仍然会遇到“404未找到”错误。请尝试此方法。我的示例适用于文件夹。在这种情况下,我不使用子文件夹。否则,我可以只使用index.php,而不会出现问题。我担心这样做行不通。我仍然收到“404未找到”错误。我好像记得我以前也试过well@Crimbo这段代码在xampp本地测试,效果非常好,因此我猜如果需要,您必须将
Rewritebase
设置到正确的文件夹中。我设置了正确的
Rewritebase
,但它仍然不起作用。我开始怀疑是不是Apache2服务器本身配置不正确?我现在发现Rao Asif Raza的解决方案有效。谢谢你的帮助!嗯,似乎正在处理.htaccess,因为它在尝试访问索引时将“.php”添加到URL,但不用于任何其他路由。所以,就像你说的,也许Apache配置是错误的。您知道要设置什么Apache配置吗?即使在编写了正确的RewriteBase之后,它仍然不起作用-在没有.php扩展名的情况下访问页面时,我仍然会遇到“404 not Found”错误,请担心延迟。刚刚检查了我的实时站点htaccess文件,下面的代码正在为它工作。RewriteBase/RewriteRule^index\.php$-[L]RewriteCond%{REQUEST_FILENAME}上的RewriteEngine-f RewriteCond%{REQUEST_FILENAME}-重写规则/index.php[L]恐怕这样不行。而且,它不是“我”
RewriteCond %{REQUEST_FILENAME} !-f
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
RewriteEngine On

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# For LocalHost !.php
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=::1

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteEngine on //replacement launch
RewriteCond %{REQUEST_FILENAME} !-d //If the requested object is not a folder
RewriteCond %{REQUEST_FILENAME}\.php -f //If the requested object to append the extension php - file
RewriteRule ^(.*)$ $1.php //make the change from concatenating .php
RewriteEngine on

RewriteCond $1 !^(index\.php|resources|robots\.txt)

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L,QSA]