D
D
Denis Semenov2015-06-08 04:25:59
Apache HTTP Server
Denis Semenov, 2015-06-08 04:25:59

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>

Almost everything works the way I want, but the issue of optimization is tormenting. It turns out that each time, in order to get the CNC, the link goes through several iterations, which we would like to minimize (preferably, to one transformation :)).
Is it possible to somehow change the code above so that all the conditions are met, but the rules are at a minimum?
And one more thing: redirect http://my.site/index.html-> http://my.siteworks 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?
As a result, the question is: how can I redo the code above in order to most accurately remove www, "/", index, .html and php. from all links? And is it possible to replace RewriteRule with everywhere in http://my.siteorder http://%1to standardize for use on all your sites at once?
Unfortunately, I have already tried many options, but my knowledge is not enough to make everything perfect.
Or just tell me that there is nowhere else to edit .htaccess and I invent a bicycle with square wheels. :)
PS: How to change ErrorDocument 404 http://my.site/#error404to 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)?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
ShamblerR, 2015-06-08
@ShamblerR

I already wrote here:
klondike-studio.ru/useful_materials/standartnyy-ht...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question