Apache 删除带有.htaccess的.php扩展名
是的,我已经阅读了Apache手册并在这里进行了搜索。由于某种原因,我根本无法让它工作。最近的一次是让它删除扩展,但它指向根目录。我希望它只在包含Apache 删除带有.htaccess的.php扩展名,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,是的,我已经阅读了Apache手册并在这里进行了搜索。由于某种原因,我根本无法让它工作。最近的一次是让它删除扩展,但它指向根目录。我希望它只在包含.htaccess文件的目录中工作 我需要用.htaccess文件做三件事 我需要它来删除.php a。我有几个页面使用选项卡,看起来像page.php#tab——这可能吗 b。我有一个页面,它使用一个附加到URL的会话ID来确保您来自正确的位置,www.domain.com/download software.php?abcdefg 这可能吗?此外,
.htaccess
文件的目录中工作
我需要用.htaccess
文件做三件事
.php
page.php#tab
——这可能吗
b。我有一个页面,它使用一个附加到URL的会话ID来确保您来自正确的位置,www.domain.com/download software.php?abcdefg
这可能吗?此外,在执行此操作时,是否需要从标题nav include文件中的链接中删除.php
?IE“应该是吗?
www
,因此它不是domain.com
,而是www.domain.com/page
我会继续查找、尝试等。在子目录中是否会导致任何问题?Gumbo在堆栈溢出问题中的回答应该可以 Re 1)将.html更改为.php 是的,这是可能的,只需将
#tab
添加到URL即可
使用QSA
(查询字符串附加)可以实现这一点,请参见下文
这也应适用于子目录路径:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
Gumbo在堆栈溢出问题上的回答应该很好 Re 1)将.html更改为.php 是的,这是可能的,只需将
#tab
添加到URL即可
使用QSA
(查询字符串附加)可以实现这一点,请参见下文
这也应适用于子目录路径:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
apachemod_重写
你要找的是
描述:提供基于规则的重写引擎进行重写
动态请求URL
一般来说,mod_rewrite
的工作原理是将请求的文档与指定的正则表达式相匹配,然后在内部(在apache进程内)或外部(在客户端浏览器中)执行URL重写。这些重写可以像在内部将example.com/foo转换为example.com/foo/bar请求一样简单
Apache文档包括一个mod_rewrite
指南,我认为其中涵盖了您想要做的一些事情
强制www
子域
我想让它在每个url前加上“www”,这样它就不是domain.com而是www.domain.com/page了
《重写指南》在示例下提供了相关说明
删除尾部斜杠(第1部分)
我想从页面中删除所有尾部斜杠
我不确定您为什么要这样做,因为《重写指南》包括,即始终包括尾部斜杠。文件建议删除尾随斜杠可能会导致以下问题:
尾斜线问题
说明:
每个站长都可以唱一首关于拖尾问题的歌
在引用目录的URL上斜杠。如果它们丢失,服务器将停止运行
转储错误,因为如果您说的是/~qux/foo
而不是/~qux/foo/
然后服务器搜索名为foo的文件。因为这个文件
是一个它抱怨的目录。实际上,它试图把它自己固定在
大多数情况下,但有时这种机制需要
你。例如,在你做了很多复杂的URL之后
重写CGI脚本等
也许您可以详细说明为什么要一直删除尾部斜杠
删除.php
扩展名
我需要它来删除.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)的文件请求
如果您使用该指令,则可能会发生这种情况。但是如果您这样做,那么这个问题就会变得更加复杂,因为您将需要指令来执行文件系统级检查请求是否映射到目录或文件
这就是说,如果您删除了删除所有尾部斜杠的要求,而强制添加尾部斜杠,“no.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
但是还有另一个选项,设置DefaultType
和DirectoryIndex
指令如下:
DefaultType application/x-httpd-php
DirectoryIndex index.php index.html
更新2013-11-14-修复了上述片段,以纳入尼科雷利乌斯的观察结果
现在对hi.txt(以及其他任何内容)的请求都成功了,对example.com/test的请求将返回经过处理的test.php版本,index.php文件将再次工作
我必须对这个解决方案给予赞扬,因为我在谷歌搜索php no extension apach时发现了它
#get rid of trailing slashes
RewriteCond %{HTTP_HOST} ^(www.)?blamcast\.net$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
# 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]
<IfModule mod_rewrite.c>
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^about$ about.php [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
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
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
# 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]
Options +MultiViews
AddHandler php5-script php
AddType text/html php
## 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]
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]