D
D
Den Alex Smith2018-11-11 12:04:47
Apache HTTP Server
Den Alex Smith, 2018-11-11 12:04:47

Redirection without string replacement (mod_rewrite - RewriteRule) how to implement?

Good afternoon!
I am trying to configure the CNC in the apache2/sites-available/domain.ru.conf file for all subdomains for links of the form:
subdomain.domain.ru /somePageName .html → subdomain.domain.ru/?page= somePageName
It is required that the server understand the request as above, and the entered text was saved in the address bar.
STEP 1 (ATTEMPT 165) I
write the directives:

RewriteCond %{REQUEST_URI} !([^/]*)\.html/$
RewriteRule ^/([^/]*)\.html$ /?page=$1 [L]

I get results in the address bar (of course, nothing works):
subdomain.domain.ru/ news .html → subdomain.domain.ru/ news .html/?page= news
subdomain.domain.ru/ questions .html → subdomain.domain .ru/ questions .html/?page= questions
STAGE 2 (DIGGING FURTHER, ATTEMPTS 166-289+) I examine
the entire block, analyze:
ServerName   domain.ru
        ServerAlias  *.domain.ru
        ServerSignature Off
        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^domain\.ru [NC]
        RewriteRule ^/(.*) http://www.domain.ru/$1 [L,R] # до этого места включительно все работает

        # вот, собственно, интересующий блок преобразований
        RewriteCond %{HTTP_HOST} ^([a-z0-9_-]+)\.domain\.ru$ [NC]
        RewriteRule ^(.*)\/([^/]*)\.html http://%1.domain.ru/?page=$2 [R,L]

I get a slightly different result from what was required (the address bar is also replaced):
subdomain.domain.ru/ news .html → subdomain.domain.ru/?page= news
subdomain.domain.ru/ questions .html → subdomain.domain. ru/?page= questions
If you remove the R flag , then the page makes too many redirects and dies.
STEP 3 (RETURN TO START)
The code from stage 1 decided to modify it a little and works the same as the code from stage 2:
# было:
# RewriteCond %{REQUEST_URI} !([^/]*)\.html/$
# RewriteRule ^/([^/]*)\.html$ /?page=$1 [L]

# поменял REQUEST_URI на REQUEST_FILENAME
# и добавил в RewriteRule флаг R
# стало:
RewriteCond %{REQUEST_FILENAME} !([^/]*)\.html/$
RewriteRule ^/([^/]*)\.html$ /?page=$1 [R,L]

The result is the same as in E.2:
subdomain.domain.ru/ news .html → subdomain.domain.ru/?page= news
subdomain.domain.ru/ questions .html → subdomain.domain.ru/?page= questions
Please help...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor Taran, 2018-11-12
@shambler81

RewriteCond %{HTTP_HOST} !^www\.domain\.ru [NC]
# if this is ! = not the domain itself with www
RewriteCond %{HTTP_HOST} ^(www\.|).+\.domain\.ru [NC]
# if it is www or without vwww, plus it is 1 or more characters before the main domain, dots are escaped.
RewriteCond %{REQUEST_URI} ^(.+)\.(html|htm)$
# well, I wrote that, you can do it too
RewriteCond %{REQUEST_FILENAME} !-d
# And this is not a directory.
RewriteCond %{REQUEST_FILENAME} !-f
# is not a file.
RewriteCond %{REQUEST_FILENAME} -l
# and not a link, in general it's not a physical object
RewriteRule ^(.*)$ / http://domain.ru/\?page\=%2/? [R=301,L]
# A group with a rewrite gond is not bucks but percentages, the principle is the same, now we substitute the domain in the steering wheel, and most importantly we shield it? this is actually a special character. which by the way we have at the end, it will remove all get parameters.
Naturally, we prescribe the domain explicitly, since we need to change it, and this is not part of the url.
If you want to save the protocol, then the rule will be overgrown with 3 more lines.

D
dodo512, 2018-11-12
@dodo512

ServerName   domain.ru
    ServerAlias  *.domain.ru
    ServerSignature Off
    
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^domain\.ru [NC]
    RewriteRule ^/(.*) http://www.domain.ru/$1 [L,R=301]
    
    RewriteCond %{HTTP_HOST} ^(www\.)?+.+\.domain\.ru$ [NC]
    RewriteRule ^/(.*)\.html$ /index.php?page=$1 [L]

Before checking, do not forget to reset the browser cache.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question