Question

If my url will like this:

http://id.factor.ua/bez-rubriki/checkout/?price=...

And url will contains bez-rubriki

How I can redirect using .htaccess to this url BUT without this slug, like next URL:

http://id.factor.ua/checkout/?price=...
Was it helpful?

Solution

As simple as this for an internal rewrite:

RewriteEngine on
RewriteRule ^bez-rubriki/checkout/$ /checkout/ [L,QSA]

For an external redirection add the R flag:

RewriteEngine on
RewriteRule ^bez-rubriki/checkout/$ /checkout/ [R=301,L,QSA]

The above are the versions to be used inside .htaccess style files.

A general note: if you have control over the http server configuration, then you should always prefer to place such rules inside the host configuration instead of using .htaccess style files. Those files are notoriously error prone, make things complex, are hard to debug and really slow the server down. They should only be used if there is no control of the http server configuration or if some app requires dynamic changes to the configuration.

So in case you want to place those rules in the host configuration you need a small modification. You have to include the leading slash (/) into the regex testing the request path:

RewriteEngine on
RewriteRule ^/bez-rubriki/checkout/$ /checkout/ [L,QSA]

And the version doing an external redirection:

RewriteEngine on
RewriteRule ^/bez-rubriki/checkout/$ /checkout/ [R=301,L,QSA]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top