URL redirection - redirection without file extension

I am currently using the following rules in a .htaccess file:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

This works well to ensure that it /myfile.phpworks as well as simply myfile(without an extension in the url). It also processes requests without problems, so it myfile?var=fooalso works.

The problem is that they are registered in Google Analytics as two separate files. Thus, although it myfile.phpmay be the third most popular page on my site with a visit to X, it myfilemay be the fifth most popular page on my site with a visit to Y.

How can I perform hard redirects rather than “accept one” type of rule?

+3
source share
4 answers

Try this rule to redirect requests for files .php:

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

Could you just change your last RewriteRule to do this?

RewriteRule ^(.*)$ $1.php [L,R=301]

This will redirect "myfile" to "myfile.php".

In addition, you can configure this inside Google Analytics using rules. Although this is probably not an ideal solution.

ETA . If you want to redirect myfile.php to myfile, try this instead of the last rule:

RewriteRule ^(.+)\.php$ $1 [L,R=301]
+2
source
+1

... , .

// The Original
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

// Add This
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php /$1 [R=301,L]

: http://www.webmasterworld.com/apache/3961636.htm

0

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


All Articles