C
C
CPAKing2021-08-27 23:28:33
htaccess
CPAKing, 2021-08-27 23:28:33

How to make a 301 redirect extracting the name of the folder where the file is located?

Greetings colleagues! There is a non-trivial task, namely to make a redirect with the name of a folder in the domain by passing it to a variable.

For example, there is a link like site1.ru/xxx and before clicking on it, you need to redirect the user to the link site2.ru/a?link=xxx, i.e. extract the folder name from the original domain and put it in a variable to another.

Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2021-08-28
@dodo512

RedirectMatch 301 ^/(.+) http://site2.ru/a?link=$1
Or

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)?site1\.ru$ [NC]
RewriteRule (.+) http://site2.ru/a?link=$1 [R=301,L]

V
Viktor Taran, 2021-08-28
@shambler81

RewriteCond %{HTTP_HOST} ^site1\.ru$
RewriteRule ^(.+)$ https://site2.ru/a\?link=$1[L,R=301]

If the domains are not mirrors, then
RewriteCond %{HTTP_HOST} ^site1\.ru
can be deleted
.
Also don't forget
folder name in the domain

THIS is a URL and not the name of a folder in the domain,
but the fact that after the question from the point of view of mod_rewrite is not a URL, it is already GET
And that
DOMAIN-URL- GET,
so to speak, is a bit of a mat part

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question