End slash and start www

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]

Therefore, my .php files can be called without the .php extension.

But I would like to be called only with the final slash. Therefore, when this trailing slash is not specified, it must be added with 301. The problem is that this creates problems with the initial www and the internal .php extension (sometimes this adds the .php recursively).

How can I do that?

Thank!

+3
source share
1 answer

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

RewriteCond %{REQUEST_URI} !\.php$
+3
source

Source: https://habr.com/ru/post/1732750/


All Articles