Remove .php extension with .htaccess
是的,我已经阅读了Apache手册并在此处进行了搜索。由于某种原因,我根本无法使它正常工作。我最接近的是删除扩展名,但它指向根目录。我希望它只在包含
我需要对
我需要它来删除.php
一种。我有几个使用标签的页面,URL看起来像page.php#tab-这可能吗?
b。我有一个页面使用将会话ID附加到URL的页面,以确保您来自正确的位置
这可能吗?另外,是否需要从我的头文件导航包含文件中的链接中删除" .php"?应该
我会继续寻找,尝试等。在子目录中会引起任何问题吗?
Gumbo在Stack Overflow问题中的答案如何使用Apache mod_rewrite隐藏.html扩展名应该可以正常工作。
关于1)将.html更改为.php
是的。可以,只需将
关于b)使用
这也应该在子目录路径中工作:
1 2 | RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L] |
Apache mod_rewrite
您正在寻找的是mod_rewrite,
好。
Description: Provides a rule-based rewriting engine to rewrite
requested URLs on the fly.Ok.
一般来说,
好。
Apache文档包含一个
好。
强制
I would like it to force"www" before every url, so its not domain.com but www.domain.com/page
Ok.
重写指南在Canonical Hostname示例下提供了有关说明。
好。
删除尾部斜杠(第1部分)
I would like to remove all trailing slashes from pages
Ok.
我不确定您为什么要这样做,因为重写指南中包含了一个完全相反的示例,即始终包含一个斜杠。该文档建议删除尾部斜杠具有引起问题的巨大潜力:
好。
Trailing Slash Problem
Ok.
Description:
Ok.
Every webmaster can sing a song about the problem of the trailing
slash on URLs referencing directories. If they are missing, the server
dumps an error, because if you say/~quux/foo instead of/~quux/foo/
then the server searches for a file named foo. And because this file
is a directory it complains. Actually it tries to fix it itself in
most of the cases, but sometimes this mechanism need to be emulated by
you. For instance after you have done a lot of complicated URL
rewritings to CGI scripts etc.Ok.
也许您可以继续说明为什么要一直删除斜杠?
好。
删除
I need it to remove the .php
Ok.
我想到的最接近的方法是内部改写每个扩展名为.php的请求文档,即example.com/somepage会作为对example.com/somepage.php的请求进行处理。请注意,以这种方式进行操作将需要每个somepage实际上在文件系统上作为somepage.php存在。
好。
使用正则表达式的正确组合在某种程度上应该是可能的。但是,我可以预见到一些可能的问题,即索引页未正确请求且目录不正确匹配。
好。
例如,这将正确重写example.com/test作为对example.com/test.php的请求:
好。
1 2 | RewriteEngine on RewriteRule ^(.*)$ $1.php |
但这会使example.com无法加载,因为没有example.com/.php
好。
我猜想,如果要删除所有的斜杠,那么几乎不可能从父目录中的文件名请求中选择目录索引的请求。如何确定对目录" foobar"的请求:
好。
1 | example.com/foobar |
从对名为foobar(实际上是foobar.php)的文件的请求中
好。
1 | example.com/foobar |
如果使用
好。
就是说,如果您删除所有尾随斜杠的要求,而强制添加尾随斜杠,则" no .php extension"问题将变得更加合理。
好。
1 2 3 4 5 6 7 8 | # Turn on the rewrite engine RewriteEngine on # If the request doesn't end in .php (Case insensitive) continue processing rules RewriteCond %{REQUEST_URI} !\.php$ [NC] # If the request doesn't end in a slash continue processing the rules RewriteCond %{REQUEST_URI} [^/]$ # Rewrite the request with a .php extension. L means this is the 'Last' rule RewriteRule ^(.*)$ $1.php [L] |
这仍然不是完美的-每个文件请求仍然在内部附加.php。请求" hi.txt"会将其放入您的错误日志中:
好。
1 | [Tue Oct 26 18:12:52 2010] [error] [client 71.61.190.56] script '/var/www/test.peopleareducks.com/rewrite/hi.txt.php' not found or unable to stat |
但是还有另一种选择,设置
好。
1 2 | DefaultType application/x-httpd-php DirectoryIndex index.php index.html |
2013年11月14日更新-修复了上面的代码片段,以整合尼古拉的观察
好。
现在,对hi.txt(以及其他所有内容)的请求成功了,对example.com/test的请求将返回test.php的已处理版本,并且index.php文件将再次运行。
好。
我必须在此解决方案应归功的地方给予信誉,因为我通过在Google上搜索php no extension apache找到了Michael J. Radwins Blog。
好。
删除尾部斜杠
对
好。
该解决方案似乎相当琐碎,使用
好。
这是他的示例,假设您的域为blamcast.net,并允许请求以
好。
1 2 3 | #get rid of trailing slashes RewriteCond %{HTTP_HOST} ^(www.)?blamcast\.net$ [NC] RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L] |
现在我们到了某个地方。让我们把它们放在一起,看看它是什么样子。
好。
强制
假设该域为foobar.com,并且它在标准端口80上运行。
好。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # Process all files as PHP by default DefaultType application/x-httpd-php # Fix sub-directory requests by allowing 'index' as a DirectoryIndex value DirectoryIndex index index.html # Force the domain to load with the www subdomain prefix # If the request doesn't start with www... RewriteCond %{HTTP_HOST} !^www\.foobar\.com [NC] # And the site name isn't empty RewriteCond %{HTTP_HOST} !^$ # Finally rewrite the request: end of rules, don't escape the output, and force a 301 redirect RewriteRule ^/?(.*) http://www.foobar.com/$1 [L,R,NE] #get rid of trailing slashes RewriteCond %{HTTP_HOST} ^(www.)?foobar\.com$ [NC] RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L] |
好。
redirect|R [=code] (强制重定向)前缀替换为
http://thishost[:thisport]/ (使新URL成为URI)强制
外部重定向。如果未给出代码,则HTTP响应为302
(已临时移动)将被返回。好。
blockquote>
最后说明
我无法成功去除斜线。 重定向最终给了我无限的重定向循环。 仔细阅读原始解决方案后,我会感觉到上面的示例适用于他们,因为他们的Drupal安装是如何配置的。 他具体提到:
好。
On a normal Drupal site, with clean URLs enabled, these two addresses
are basically interchangeableOk.
关于以斜杠结尾和不以斜杠结尾的URL。 此外,
好。
Drupal uses a file called
.htaccess to tell your web server how to
handle URLs. This is the same file that enables Drupal's clean URL
magic. By adding a simple redirect command to the beginning of your
.htaccess file, you can force the server to automatically remove any
trailing slashes.Ok.
好。
除了上述其他答案之外,
您也可以尝试从文件中完全删除.php扩展名,并避免无限循环:
1
2
3
4
5
6
7 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。编辑:
在Apache 2.4和更高版本上,您还可以使用END标志来防止无限循环错误。以下示例与apache 2.4上的示例相同,
1
2
3
4
5 RewriteEngine on
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
以下代码对我来说很好用:
1
2
3
4 RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
如果您只想对一个特定文件执行以下操作,则可以使用以下方法:
1 RewriteRule ^about$ about.php [L]参考:http://css-tricks.com/snippets/htaccess/remove-file-extention-from-urls/
将
/etc/apache2/apache2.conf (Debian 8)中的AllowOverride 参数从None 更改为All 之后,.htaccess文件仅必须包含:
1
2
3 Options +MultiViews
AddHandler php5-script php
AddType text/html php足以从文件中隐藏.php扩展名
不知道为什么其他答案对我不起作用,但是我发现的这段代码确实有用:
1
2
3 RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]这就是我的htaccess中的全部内容,而example.com/page显示了example.com/page.php
要从PHP文件中将.php扩展名删除,例如yoursite.com/about.php到yoursite.com/about,请执行以下步骤。
从您的网站的根目录打开.htaccess(如果不存在,则创建一个新文件),并添加以下代码。
1
2
3 RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]要将html文件中的.html扩展名从例如yoursite.com/about.html删除到yoursite.com/about,请执行以下步骤。
从您的网站的根目录打开.htaccess(如果不存在,则创建一个新文件),并添加以下代码。
1
2
3 RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]参考:如何从URL中删除php扩展名
尝试这个
以下代码肯定会起作用
1
2
3
4
5
6 RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
这是我用来从文件名隐藏
.php 扩展名的代码:
1
2
3
4 ## hide .php extension
# To redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L,NC]注意:
R=301 用于永久重定向,建议用于SEO。但是,如果只想进行临时重定向,请仅用R 替换它
如果您使用PHP进行编码,并且想要删除.php,则可以使用如下网址:
http://yourdomain.com/blah->指向/blah.php
这就是您所需要的:
1
2
3 <IfModule mod_rewrite.c>
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
我发现100%可以为我工作:
1
2
3
4
5
6
7
8
9 # Options is required by Many Hosting
Options +MultiViews
RewriteEngine on
# For .php & .html URL's:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]在您的网站.htaccess文件的根目录中使用以下代码:
离线-wamp www YourWebDir
在线-public_html /
If it doesn't work correct, then change the settings of your Wamp
Server: 1) Left click WAMP icon 2) Apache 3) Apache Modules 4) Left
click rewrite_module
如果您在PHP中的网址(例如http://yourdomain.com/demo.php)比
这就是您所需要的:
创建文件.htaccess
1
2
3
4 RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]