尾随斜线和初始WWW斜线、WWW

2023-09-02 09:45:54 作者:红颜枯骨

我有这样的htaccess的:

I have this htaccess:

  RewriteEngine On

  # redirect with www
  RewriteCond %{HTTP_HOST} ^mydomain [NC]
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1/ [R=301,L]

  # add .php internally
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ $1.php [L,QSA]

所以,我的PHP文件可以被称为不带.php扩展。

So my .php files can be called without the .php extension.

不过,我希望他们可以只用斜线调用。所以,当这最后的斜线没有给出,就应该用301这个问题我已经附加的是,这是给我的问题与最初的WWW,和PHP扩展本身(有时是加入递归的.php)。

But I'd like them to be called only with a trailing slash. So when this trailing slash is not given, it should be appended with a 301. The problem I have is that this is giving me problems with the initial www, and the .php extension itself (sometimes it is adding recursively .php).

如何能不能做到?

谢谢!

推荐答案

我想你需要添加这样的事情你最后重写规则之前,为避免重复已经以.php结束的URI

I think you need to add something like this before your last rewrite rule, to avoid rewriting URIs that already end in .php

RewriteCond %{REQUEST_URI} !.php$