L
L
lilikon2018-10-24 11:48:26
htaccess
lilikon, 2018-10-24 11:48:26

How to redirect a subdomain to one page using htaccess?

There is a site: site.ru
There is a subdomain for this site: sub.site.ru
There are pages: index.php, sub.php
htaccess for the main site is configured as follows (works as it should):


RewriteRule ^status/([^/]+)/?$ index.php?action=weekly&status=$1 [L]
RewriteRule ^status/?$ index.php?action=weekly [L]

When you enter site.ru/status/ - opens the same as site.ru/index.php?action=weekly
When you enter site.ru/status/1/ - opens the same as site.ru/index.php ?action=weekly&status=1
This part works great!
The task is to connect ONE page to the subdomain - sub.php
What should happen:
sub.site.ru - opens the same as site.ru/sub.php?action=weekly
sub.site.ru/1/ - opens the same just like site.ru/sub.php?action=weekly&status=1
Is it possible to implement this?
If yes, then I ask for your help.
What I have tried:

RewriteCond %{HTTP_HOST} ^sub\.([^/]+)\.ru/? [NC]
RewriteRule ^(.*)/([^/]+)/?$ sub.php?action=weekly&status=$1 [L]
RewriteRule ^(.*)/?$ sub.php?action=weekly [L ]

When I go to sub.site.ru - opens sub.php?action=weekly (correctly, it works)
BUT when I try to go to sub.site.ru/1/ - opens as before sub.php?action=weekly, and should open sub.php?action=weekly&status=1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Taran, 2018-10-24
@lilikon

well, for starters, a little theory
? - and then this is a GET parameter, it is not part of the RequestURI and, as a result, is not visible to them at all, so redirects using GET parameters need to be done differently, and in addition, escape special characters, they are for this

\?
\/=

they won't die from it for sure.
Thirdly, you are trying to write CNC - I recommend not to invent a bicycle, but to use it.
Well, let's find out.
RewriteCond %{HTTP_HOST} ^(www\.|)sub\.(.+) [NC]
#Если хост www.sub или sub.и далее хотябы один символ, плюс все регистронезависимое
RewriteRule ^(.*)$ http://sub.php\?action\=weekly\&status\=$1? [L]
#то выделяем все что там после домена в группу1 и не забывая экранировать спецсимволы подставляем в урл, плюс в конце стоит вопрос, это блядство удалит гет параметры   выставленные до этого ;)

If you need to use the GET parameter in this url, then
it will be possible to Catch it already through
Collect it in the same way in a group and substitute it in the steering wheel, but not $2 but %2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question