[英]Remove .php extension, force non-slash, force non-www, force https with .htaccess
I have been looking into this for quite a while now, but i just can't get it right. 我已经研究了很长时间了,但是我做对了。 When I have one part working (eg forcing non-php), the other part stops working (eg forcing non-slash). 当我有一部分工作(例如,强制非PHP)时,另一部分停止工作(例如,强制非斜杠)。
What I want exactly: 我到底要什么:
example.com/foo.php
becomes example.com/foo
in the browser url bar) 删除.php扩展名(因此example.com/foo.php
在浏览器网址栏中成为example.com/foo
) example.com/foo.php/
becomes example.com/foo
. BUT when it's a directory with an index.php inside, it has to redirect accordingly (i can't get this last part to work) 删除URL末尾的斜杠(因此example.com/foo.php/
变为example.com/foo
。但是当它是一个内部带有index.php的目录时,它必须进行相应的重定向(我无法获得此重定向)工作的最后一部分) What I my .htaccess looks like right now: 我现在的.htaccess内容如下所示:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I experience the following problems with this: 我遇到以下问题:
example.com/foo.php/
redirects to example.com/foo.php
instead of example.com/foo
example.com/foo.php/
重定向到example.com/foo.php
而不是example.com/foo
example.com/foo.php/foo
displays the example.com/foo.php
file, while example.com/foo/foo
displays a 404 message (the latter would be correct in my case) example.com/foo.php/foo
显示example.com/foo.php
文件,而example.com/foo/foo
显示404消息(在我看来,后者是正确的)
example.com/admin
(this is a folder with an index.php in it) displays a 404 message example.com/admin
(这是其中带有index.php的文件夹)显示404消息
I appreciate your help! 我感谢您的帮助!
Okay, so I know it is a broad question. 好的,我知道这是一个广泛的问题。 But if anyone ever searches for this, I almost have it (the only thing that doesn't work is redirecting domain.com/foo.php/foo
to domain.com/foo/foo
但是,如果有人搜索过此文件,我几乎就会domain.com/foo.php/foo
它(唯一不起作用的是将domain.com/foo.php/foo
重定向到domain.com/foo/foo
Here is the .htaccess file I have right now: 这是我现在拥有的.htaccess文件:
RewriteEngine On
Options -Indexes
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^/]+)\.php(\?|\ |$)
RewriteRule ^ /%2 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.