Answer the question
In order to leave comments, you need to log in
How to optimize redirects in htaccess?
Hey!
I read a lot of information on the Internet, how to make CNC using .htaccess, and I wanted to make a template for my needs.
As a result, I compiled the following version:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
#1. Убрать www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#2. Убрать / в конце адреса
RewriteCond %{HTTP_HOST} (.*)
RewriteCond %{REQUEST_URI} /$ [NC]
RewriteRule ^(.*)(/)$ $1 [L,R=301]
#3. Убрать /index.html или /index.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(html|php)\ HTTP/
RewriteRule ^index\.(html|php)$ http://my.site[R=301,L]
#4. Убрать .html или .php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.(html|php)\ HTTP
RewriteRule ^([^.]+)\.(html|php)$ http://my.site/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.html [L]
</IfModule>
http://my.site/index.html
-> http://my.site
works fine, but if you enter a link in the browser http://my.site/index
(without .html), then the tail in the form of index remains. You can do it standardly: Redirect 301 /index http://my.site
, but then you get even more conversion steps, which we would not like. Tell me how to overcome the tail in this case?http://my.site
order http://%1
to standardize for use on all your sites at once? ErrorDocument 404 http://my.site/#error404
to remove the address ( my.site ) from .htaccess, but the redirect was to the main page of the site with the addition of /#error404 (or another "error identifier" in the address, when the user is on the main site page)? Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question