当前位置:  开发笔记 > 编程语言 > 正文

使用.htaccess删除.php扩展名

如何解决《使用.htaccess删除.php扩展名》经验,为你挑选了7个好方法。

是的,我已阅读Apache手册并在此搜索.出于某种原因,我根本无法让这个工作.我最接近的是删除扩展名,但它指向根目录.我希望这只是在包含该.htaccess文件的目录中工作.

我需要对.htaccess文件做三件事.

    我需要它来删除.php

    一个.我有几个页面使用选项卡,URL看起来像page.php #tab - 这可能吗?

    湾 我有一个页面使用附加到URL的会话ID,以确保您来自正确的地方,www.domain.com/download-software.php?abcdefg.

这可能吗?另外,在执行此操作时,是否需要从头文件导航包含文件中的链接中删除".php"?应该IE "support" be support?

    我希望它在每个URL之前强制使用"www",所以它不是domain.com,但是www.domain.com/page.

    我想从页面中删除所有尾部斜杠.

我会继续寻找,尝试等等.会在子目录中导致任何问题吗?



1> Pekka suppor..:

Gumbo在Stack Overflow问题中的答案如何使用Apache mod_rewrite隐藏.html扩展名应该可以正常工作.

重新1)将.html更改为.php

重新a.)是的,这是可能的,只需添加#tab到URL.

Re b.)可以使用QSA(Query String Append),见下文.

应该也适用于子目录路径:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]


@Pekka웃删除`[L,`space`QSA]`标志之间的空格,否则_500内部服务器错误_将导致.

2> Tim Bielawa..:
Apache mod_rewrite

你要找的是mod_rewrite,

描述:提供基于规则的重写引擎,以便动态重写请求的URL.

一般来说,mod_rewrite通过将所请求的文档与指定的正则表达式进行匹配,然后在内部(在apache进程内)或外部(在客户端浏览器中)执行URL重写.这些重写可以像将example.com/foo内部翻译成example.com/foo/bar的请求一样简单.

Apache文档包含一个mod_rewrite指南,我认为你想要做的一些事情都包含在内.详细的mod_rewrite指南.

强制www子域

我希望它在每个网址之前强制使用"www",所以它不是domain.com而是www.domain.com/page

重写指南包含Canonical Hostname示例下的相关说明.

删除尾部斜杠(第1部分)

我想从页面中删除所有尾部斜杠

我不确定你为什么要这样做,因为重写指南包含一个完全相反的例子,即始终包含一个尾部斜杠.文档表明删除尾部斜杠很有可能导致问题:

拖尾斜线问题

描述:

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.

Perhaps you could expand on why you want to remove the trailing slash all the time?

Remove .php extension

I need it to remove the .php

我能想到的最接近这一点的是在内部重写每个扩展名为.php的请求文档,即example.com/somepage被处理为example.com/somepage.php的请求.请注意,以这种方式进行将要求每个somepage实际上作为somepage.php存在于文件系统上.

通过正则表达式的正确组合,这在某种程度上是可能的.但是,我可以预见到索引页面没有被正确请求并且没有正确匹配目录的一些可能问题.

例如,这将正确地重写example.com/test作为example.com/test.php的请求:

RewriteEngine  on
RewriteRule ^(.*)$ $1.php

但是会使example.com无法加载,因为没有example.com/.php

我猜想如果你要删除所有尾部斜杠,那么从父目录中的文件名请求中选择一个目录索引请求几乎是不可能的.如何确定目录'foobar'的请求:

example.com/foobar

来自对foobar文件的请求(实际上是foobar.php)

example.com/foobar

如果您使用该RewriteBase指令可能是可能的.但是,如果你这样做,那么这个问题变得更加复杂,因为RewriteCond如果请求映射到目录或文件,你将需要指令来进行文件系统级别检查.

也就是说,如果您删除了删除所有尾部斜杠的要求,而是强制添加尾部斜杠,则"无.php扩展名"问题会变得更加合理.

# 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'的请求会将此信息放入您的错误日志中:

[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

但还有另一种选择,设置如下的DefaultTypeDirectoryIndex指令:

DefaultType application/x-httpd-php
DirectoryIndex index.php index.html

更新2013-11-14 - 修正了上面的片段,以纳入nicorellius的观察

现在对hi.txt(以及其他任何内容)的请求成功,对example.com/test的请求将返回test.php的处理版本,index.php文件将再次起作用.

我必须给予信用这个解决方案的信用,因为我发现Michael J. Radwins博客搜索谷歌的php没有扩展apache.

删除尾部斜杠

Some searching for apache remove trailing slashes brought me to some Search Engine Optimization pages. Apparently some Content Management Systems (Drupal in this case) will make content available with and without a trailing slash in URls, which in the SEO world will cause your site to incur a duplicate content penalty. Source

The solution seems fairly trivial, using mod_rewrite we rewrite on the condition that the requested resource ends in a / and rewrite the URL by sending back the 301 Permanent Redirect HTTP header.

Here's his example which assumes your domain is blamcast.net and allows the the request to optionally be prefixed with www..

#get rid of trailing slashes
RewriteCond %{HTTP_HOST} ^(www.)?blamcast\.net$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

Now we're getting somewhere. Lets put it all together and see what it looks like.

Mandatory www., no .php, and no trailing slashes

This assumes the domain is foobar.com and it is running on the standard port 80.

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

The 'R' flag is described in the RewriteRule directive section. Snippet:

redirect|R [=code] (force redirect) Prefix Substitution with http://thishost[:thisport]/ (which makes the new URL a URI) to force a external redirection. If no code is given, a HTTP response of 302 (MOVED TEMPORARILY) will be returned.

Final Note

I wasn't able to get the slash removal to work successfully. The redirect ended up giving me infinite redirect loops. After reading the original solution closer I get the impression that the example above works for them because of how their Drupal installation is configured. He mentions specifically:

On a normal Drupal site, with clean URLs enabled, these two addresses are basically interchangeable

In reference to URLs ending with and without a slash. Furthermore,

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.



3> starkeen..:

除了上面的其他答案,

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

编辑:

在apache 2.4及更高版本中,您还可以使用END标志来防止无限循环错误.以下示例与apache 2.4上的相同,

RewriteEngine on

RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]


嗨,谢谢你.我已经尝试了许多解决方案来削减.php,但只有这个工作正常:)
谢谢!这个编辑是它的位置,apache 2.4,apache 2.5最终进入无限循环(并且将会500)[END]超级重要

4> user1079877..:

以下代码对我来说很好:

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



5> sbuck..:

如果您只想为一个特定文件执行此操作,请使用以下方法:

RewriteRule ^about$ about.php [L]

参考:http://css-tricks.com/snippets/htaccess/remove-file-extention-from-urls/



6> Rodney Salce..:

更改参数后AllowOverride,从NoneAll/etc/apache2/apache2.conf(Debian的8),下面这个,.htaccess文件只是必须包含:

Options +MultiViews
AddHandler php5-script php
AddType text/html php

它足以隐藏文件中的.php扩展名



7> Jon..:

不知道为什么其他答案对我不起作用,但我发现这个代码做了:

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

这就是我的htaccess中的所有内容,example/page显示了example.com/page.php

推荐阅读
手机用户2402852387
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有