正在尝试删除.php文件扩展名
我刚刚在我的ubuntu apache服务器上安装了mod_rewrite并启用了.htaccess。 但是当我编辑.htaccess以从文件中删除.php文件扩展名时,url不会改变 现在,我的.htaccess文件如下所示:正在尝试删除.php文件扩展名,php,apache,.htaccess,mod-rewrite,ubuntu,Php,Apache,.htaccess,Mod Rewrite,Ubuntu,我刚刚在我的ubuntu apache服务器上安装了mod_rewrite并启用了.htaccess。 但是当我编辑.htaccess以从文件中删除.php文件扩展名时,url不会改变 现在,我的.htaccess文件如下所示: Options -Indexes ErrorDocument 400 /blabla.php ErrorDocument 401 /blabla.php ErrorDocument 403 /blabla.php ErrorDocument 404 /blabla.p
Options -Indexes
ErrorDocument 400 /blabla.php
ErrorDocument 401 /blabla.php
ErrorDocument 403 /blabla.php
ErrorDocument 404 /blabla.php
ErrorDocument 500 /blabla.php
没有太多,只是确保ppl不能查看我的文件树,并添加了一些自定义错误页面
但是,如果我在我的网站上输入一个页面,例如href=“page.php”,一切正常,但是如果我从url栏中删除“.php”,并将页面重新加载为“www.mysite.com/page”,而不使用“.php”,那么它仍然正常工作。我在.htaccess文件中没有做任何操作,对吗
如果我现在向.htaccess添加以下行:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Options -MultiViews
Options -MultiViews
RewriteEngine On
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
然后访问href=“page.php”url仍然显示为“www.mysite.com/page.php”,而不是“www.mysite.com/page”
我是否还需要将href从href=“page.php”更改为href=“page”?为了使.php扩展消失。-因为即使我一开始不编辑.htaccess文件,它也可以工作
我总是确保在编辑.htaccess文件时重新启动apache
先谢谢你,K
page
自动呈现您已启用的page.php
,很可能特别是httpd.conf
级别的MultiViews
page
自动呈现您已启用的page.php
,很可能特别是httpd.conf
级别的MultiViews
你在这里问了很多问题。让我回答第一个问题:
www.mysite.com/page
由于选项多视图
的原因,在没有任何重写规则的情况下工作。选项MultiViews
由Apache的内容协商模块使用,该模块在mod_rewrite
之前运行,并使Apache服务器匹配文件扩展名。所以/file
可以在URL中,但它将提供/file.php
如果将此行放在根.htaccess的顶部:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Options -MultiViews
Options -MultiViews
RewriteEngine On
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
然后www.mysite.com/page
将发布404
另外,要从URL中自动删除.php
,请在root.htaccess中使用以下代码:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Options -MultiViews
Options -MultiViews
RewriteEngine On
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
你在这里问了很多问题。让我回答第一个问题:
www.mysite.com/page
由于选项多视图
的原因,在没有任何重写规则的情况下工作。选项MultiViews
由Apache的内容协商模块使用,该模块在mod_rewrite
之前运行,并使Apache服务器匹配文件扩展名。所以/file
可以在URL中,但它将提供/file.php
如果将此行放在根.htaccess的顶部:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Options -MultiViews
Options -MultiViews
RewriteEngine On
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
然后www.mysite.com/page
将发布404
另外,要从URL中自动删除.php
,请在root.htaccess中使用以下代码:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Options -MultiViews
Options -MultiViews
RewriteEngine On
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
试试这个
RewriteRule (.*)\ $1.php
试试这个
RewriteRule (.*)\ $1.php
同时检查mod_rewrite
是否已启用:sudo a2enmod rewrite
可能的重复:同时检查mod_rewrite
是否已启用:sudo a2enmod rewrite
可能的重复:它工作正常,但链接被截断,我的url显示为“www.mysite.com:8080/page”不知道为什么“:8080”出现,可能是因为我安装了varnish造成的吗?是的,至少这些规则中没有一个在任何地方添加:8080
。好的,所以如果我的href看起来像href=“page.php”,我将被重定向到“www.mysite.com:8080/page”,这不会起作用。。但是如果我的href是href=“page”,我会被重定向到“www.mysite.com/page”,并且它可以工作。只要我将.php添加到url并重新加载页面,它就会将我重定向到:8080尝试替换如下链接:href=“/page.php”
谢谢你的回答,但这对我没有帮助。当.php在链接中时,它会被破坏。我认为这与apache/varnish有关。Prob Apache在公共界面上监听,而不是在环回界面上监听。它可以工作,但链接会断开,我的url会显示为“www.mysite.com:8080/page”不知道为什么会显示“:8080”,这可能是因为我安装了varnish?是的,至少这些规则中没有一个在任何地方添加:8080
。好的,如果我的href看起来像href=“page.php”我将被重定向到“www.mysite.com:8080/page”,但这不起作用..但是如果我的href是href=“page”,我将被重定向到“www.mysite.com/page”并起作用。只要我将.php添加到url并重新加载页面,它就会将我重定向到:8080尝试替换你的链接,如下图所示:href=“/page.php“
谢谢你的回答,但这对我没有帮助。当.php在链接中时,它会被破坏。我认为这与apache/varnish有关。Prob Apache在公共接口上侦听,而不是在环回接口上侦听。