从URL中删除.php

时间:2015-04-09 15:21:01

标签: php apache .htaccess mod-rewrite

Ubuntu 14.04LTS 32bit

LAMP

我知道这是一个古老的问题,但是......

我需要它从可见网址中找到的地方删除.php。 它需要使用/showthread.php?id=XX ---> / showthread?ID = XX

我甚至无法使用/page.php - > /page。 我试过这些:

Remove .php extension with .htaccess

How to hide the .html extension with Apache mod_rewrite

Remove .php from urls with htaccess

How to stop .htaccess loop

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

虽然

<?php 
phpinfo();

列出已加载模块中的mod_rewrite

<?php
 if(!function_exists('apache_get_modules') ){ phpinfo(); exit; }
 $res = 'Module Unavailable';
 if(in_array('mod_rewrite',apache_get_modules())) 
 $res = 'Module Available';
?>
<html>
<head>
<body>
<p><?php echo apache_get_version(),"</p><p>mod_rewrite $res"; ?></p>
</body>
</html>

返回Module Available

尝试了更多的事情

# Apache Rewrite Rules
 <IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /

# Add trailing slash to url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ $1/ [R=301,L]

# Remove .php-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^([^\.]+)/$ $1.php 

# End of Apache Rewrite Rules
 </IfModule>

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

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

这甚至没有任何影响:

RewriteRule ^page$ page.php [L]

sudo service apache2 restart不会改变任何内容。

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

我尝试清除其他代码,没有做任何改动。

我清除了浏览器缓存100次

我开始认为它只是讨厌我。有什么可能导致这个?

9 个答案:

答案 0 :(得分:5)

检查this link

这是使用.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上进行测试并正常工作。

答案 1 :(得分:2)

尝试这样可以从文件中完全删除.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。

答案 2 :(得分:0)

也许这可能是重复的,但请看一下:

How to remove .html from URL

只需在解决方案中更改hmtl

php即可

答案 3 :(得分:0)

你可能走在正确的轨道上。但是,听起来你的.htaccess文件没有被执行。仅仅因为模块被激活,并不意味着它在您的特定情况下可用。

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

  • 首先,仔细检查拼写 。验证拼写是否正确(包括开头的。)
  • 检查文件权限。某些服务器将需要可执行权限。所以chmod文件为755。
  • 如果你还没有工作,请进入你的apache配置(可能在Ubuntu上的/etc/apache2/apache2.conf)并找到AllowOverride的每个实例。可能会设置为“无”。将其更改为AllowOverride all
  • 然后进入已启用网站,找到您的网站配置,并更改AllowOverride字段。
  • 重新启动Apache服务器并用一大杯咖啡祝贺自己。

其中一个应该解决它。我建议在每个步骤之间尝试,以便您可以确定错误发生的位置。确定原因后,您可能需要返回并限制其中一些AllowOverrides,具体取决于您的需求。

祝你好运!

答案 4 :(得分:0)

您可以尝试以下操作。

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php$2 [L]

答案 5 :(得分:0)

它对我有用:

    #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]

答案 6 :(得分:0)

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

答案 7 :(得分:0)

在我的Apache配置中启用mod_negotiation为我做了诀窍:

  

内容协商,或者更准确地说是内容选择,是从几个可用文档中选择一个与客户端功能最匹配的文档。

答案 8 :(得分:0)

正在运行,您可以尝试使用此代码。

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]