A
A
Andrey Andreevich2019-03-05 22:47:03
Apache HTTP Server
Andrey Andreevich, 2019-03-05 22:47:03

htaccess regular expressions, how to select specific directories and redirect with QSA?

Gentlemen, help optimize .htaccess in such a way as to avoid duplicate RewriteRule for each of the folders.
I send the client the layout sources as a link to the technical subdomain, file listing is allowed in the root
Options +Indexes

Whole .htaccess file
Options +Indexes
IndexOptions Charset=utf8
DirectoryIndex myindex.php

<IfModule mod_autoindex.c>
IndexOptions IgnoreCase FancyIndexing FoldersFirst NameWidth=* DescriptionWidth=* XHTML HTMLtable SuppressHTMLPreamble 
  HeaderName /myFolder/.dirlist_src/dirlist_HEADER.html
  ReadmeName /myFolder/.dirlist_src/dirlist_FOOTER.html
  IndexIgnore .htaccess .ftpquota .DS_Store *.php *.zip *.htaccess
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/myFolder/(css|js|fonts|img)/(.*)$ [NC]
RewriteRule ^css(.+)$ /myFolder/.css$1  [QSA,L]
RewriteRule ^js(.+)$ /myFolder/.js$1  [QSA,L]
RewriteRule ^fonts(.+)$ /myFolder/.fonts$1  [QSA,L]
RewriteRule ^img(.+)$ /myFolder/.img$1  [QSA,L]
</IfModule>

The client has nothing to be distracted by folders with fonts, pictures or styles, therefore, I hide the extra folders by adding a dot to their name, for example /.css or /.js, so only .html files remain in the listing.
However, if you rename the folders , then you need to edit the paths in the files vertski, and this is hemorrhoids, because there can be a hundred files!
Therefore, it was decided to arrange a redirect for all the necessary files, as I understand it:
if the URL /myFolder/{N}/ is requested, the link to /myFolder/{.2N} should be replaced (with a dot);
the rule “cuts off” part of the link with {N} and substitutes the rest to /myFolder/{.2N}, the url is not supplemented in any way and is formed with the parent request.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/myFolder/(css|js|fonts|img)/(.*)$ [NC]
RewriteRule ^css(.+)$ /myFolder/.css$1  [QSA,L]
RewriteRule ^js(.+)$ /myFolder/.js$1  [QSA,L]
RewriteRule ^fonts(.+)$ /myFolder/.fonts$1  [QSA,L]
RewriteRule ^img(.+)$ /myFolder/.img$1  [QSA,L]
</IfModule>

My problem is that if for the condition I managed to write down all the folders needed to replace (css|js|fonts|img) in one line, then there is no longer a rule for the directive. I had to duplicate the rule and specify a different one for each folder. By the way, it works and copes with the task.
But I have a strong feeling that this rule can be written using regular expressions. Yes, it will become more difficult to understand, but the number of lines will decrease :)
Tell me, please, how to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2019-03-05
@am0rall

You can not rename, but simply add folder names to IndexIgnore.
Well, if you really want to do it on mod_rewrite.

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/myFolder/(css|js|fonts|img)/(.*)$ [NC]
RewriteRule ^ /myFolder/.%1/%2  [QSA,L]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question