K
K
Kostya Tester2018-11-09 12:00:09
htaccess
Kostya Tester, 2018-11-09 12:00:09

How to do the right 301 redirect?

Hi all! Guys tell me how to make a 301 redirect of this kind in htassecc? CNC works as it should, but the redirect does not want to work in any way.
Old address https://test.ru/parts/part.php?id=18427
New address https://test.ru/parts/18427
I'm doing this and it doesn't work!

RewriteEngine On
RewriteRule ^parts/([^/]*)$ /parts/part.php?id=$1 [L]
RewriteCond %{THE_REQUEST} " /parts/part\.php\?id=([^&]+)"
RewriteRule ^ https://test.ru/%1/%2/? [R=301,L,NE]


Thanks in advance for the correct answer!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaliy Orlov, 2018-11-09
@dkostia

1) you need to remove the space after the first quote in the regular expression "/parts/part\.php\?id=([^&]+)"
2) you have only one "pocket" i.e. in %2 will be empty
3) in your id=([^&]+) the transfer protocol will be captured, there will be something like "123 HTTP/1.1", i.e. you need to add a space inside,
I recommend you do like this

RewriteCond %{THE_REQUEST} "/parts/part\.php\?id=([^& ]+)"
RewriteRule . test.php?test=%1

and in test.php
<?php

print_r($_REQUEST);

debug the parameters that you receive with a regular expression, and then do a
PS redirect to see what is generally transferred to %{THE_REQUEST} and in what format, you can do this:
RewriteCond %{THE_REQUEST} "^(.+)$"
RewriteRule . test.php?test=%1

S
ShamblerR, 2018-11-09
@ShamblerR

1. GET is not part of the url and stands out separately, moreover, because %1 is used separately instead of $1, since the data is taken from COND
2. collect here
https://donatstudios.com/RewriteRule_Generator

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question