简体   繁体   English

使用.htaccess从URL删除PHP文件扩展名

[英]Using .htaccess to remove PHP file extension from URL

I just finished installing a LAMP stack on Ubuntu 12, and have run into an issue with Apache's .htaccess file. 我刚刚在Ubuntu 12上完成了LAMP堆栈的安装,并遇到了Apache的.htaccess文件问题。 I have the rewrite and redirect mods enabled, and the .htaccess file is working (the URI will redirect to 'www' if there is no 'www' present), but no matter what I try, I cannot get it to remove file extensions. 我启用了重写和重定向模块,并且.htaccess文件正在工作(如果不存在“ www”,则URI将重定向到“ www”),但是无论我如何尝试,我都无法获取它来删除文件扩展名。 I've tried the <Files> directive with no luck. 我试过<Files>指令没有运气。 My current file consists of the following: 我当前的文件包括以下内容:

RewriteEngine On

# Remove file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ $1.php [L]

# Force www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Any suggestions on how to fix this very annoying problem? 关于如何解决这个非常烦人的问题的任何建议?

You don't use htaccess to do this, you use your app to remove the extensions, and htaccess to map extension-less urls to real files. 您无需使用htaccess来执行此操作,而是使用应用程序删除扩展名,并使用htaccess将无扩展名的url映射到实际文件。 This rule 这个规则

# Remove file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

Says, "if the requested resource doesn't exist as a file, look for the resource with a .php extension". 说,“如果所请求的资源不作为文件存在,则寻找具有.php扩展名的资源”。 So you remove the extension from all links in your app, and this rule will make the php file run without the extension. 因此,您从应用程序中的所有链接中删除了扩展名,此规则将使php文件在没有扩展名的情况下运行。 Your htaccess is fine as-is, you need to update your app. 您的htaccess很好,您需要更新您的应用程序。

There is another htaccess alternative I use very successfully: 我非常成功地使用了另一个htaccess替代方法:

Options +FollowSymLinks

Options -Indexes

RewriteEngine On

RewriteRule ^purchase-jelly-babies$ /modules/products/jelly_babies.php [L]

RewriteRule ^/lets/use/an/asp/extension.asp$ /modules/test/asp_example.php [L]

This method not only solves your PHP extension issue but also allows you to keep your files organized no matter what those SEO idiots tell you what the URL should be. 这种方法不仅可以解决您的PHP扩展问题,而且还可以使您的文件井井有条,无论这些SEO笨蛋告诉您什么是URL。

# 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>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php 

Please try This code and tell me it performance. 请尝试使用此代码,并告诉我它的性能。

  RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/$ $1.php

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)/$ $1.html

RewriteCond %{REQUEST_FILENAME}.py -f
RewriteRule ^(.*)/$ $1.py

the way am doing it. 这样做的方式。

RewriteEngine on
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://domainname.com/$1 [R=301,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM