简体   繁体   English

.htaccess - 强制www,尾随斜杠并删除扩展名

[英].htaccess - force www, trailing slash and remove extension

I've been playing about with my .htaccess file and so far, it's working, but there's a few bugs. 我一直在玩我的.htaccess文件,到目前为止,它正在运行,但有一些错误。

I'm trying to force the WWW prefix (just the root, not on subdomains) while removing the .php extension and adding a trailing slash. 我试图在删除.php扩展名并添加斜杠时强制使用WWW前缀(仅是根目录,而不是子域)。

Code

# Force WWW prefix
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

RewriteRule (.*)/$ $1.php [L]


# Force trailing slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

It successfully force the WWW, even if I remove it, the .php extension is removed, even if I add it, and the the trailing slash is forced, even if I remove it. 即使删除了它,它也会成功强制WWW,即使添加了.php扩展名也会被删除,并且即使删除了它,也将强制使用尾部斜杠。 However, sometimes I get a 404 not found error saying that the requested URL (generally ending with the .php extension) was not found on the server, and often directories do not actually work. 但是,有时会收到404 not found错误,提示在服务器上找不到请求的URL(通常以.php扩展名结尾),并且通常目录实际上不起作用。 Edit 编辑

often directories do not actually work 通常目录实际上不起作用

meaning the server throws a 404 error saying "/directory.php was not found on this server". 表示服务器抛出404错误,提示“在此服务器上未找到/directory.php”。

Can anyone lend me a hand? 任何人都可以帮我一把吗?

You need to check that you're actually rewriting to a php file before you blindly attach the php extension. 在盲目附加php扩展名之前,您需要检查您是否实际上是在重写php文件。 So this rule: 所以这条规则:

RewriteRule (.*)/$ $1.php [L]

Needs some conditions: 需要一些条件:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]

You can also avoid redirecting to include www in the hostname for subdomains if you check that there is at least 1 host before the TLD, try adding another condition to your prefix rule: 如果您检查TLD之前是否至少有1个主机,您还可以避免重定向以在子域的主机名中包含www,请尝试在前缀规则中添加另一个条件:

# Force WWW prefix
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

EDIT: 编辑:

Here's what the full file should look like: 这是完整文件应该是什么样子:

RewriteBase /

# Force WWW prefix
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]


# Force trailing slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM