Trailing slash and initial www

2019-07-02 12:54发布

问题:

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]

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

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).

How can it be done?

Thanks!

回答1:

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$