A
A
Artem2013-11-18 15:16:59
htaccess
Artem, 2013-11-18 15:16:59

How can I change htaccess so that urls beyond a certain slash are simply cut off?

Good day.
There is a site on Bitrix and the directory structure has recently changed + the writing of urls was transferred to CNC. Accordingly, you need to set up a redirect from old indexed pages to new URLs.
Previously, the structure was as follows:
Catalog section - example.com/catalog/1234
Product card - example.com/catalog/1234/9876
Now the structure is this:
Catalog section - example.com/section/subsection/subsection_double_name (I know that is not very successful, but so far)
Product card - example.com/section/subsection/item_name The
redirect is now carried out through htaccess
Approximately in this form:
redirect /catalog/1234/9876/ http://example.com/section/subsection/item_name
... and so all the product cards available at the time of the transition on the site and after them the sections of the catalog separately: redirect
/catalog/1234 http://example.com/section/subsection/subsection_double_name
a product card that has already been removed from the site, for example, having ID 9999 and once located at: example.com/catalog/1234/9999,
then as a result, the redirect works to a URL of this type:
example.com/section/subsection/subsection_double_name /9999
Question - how can I tell in htaccess that urls beyond a certain slash are simply cut off? So that in the example above, the redirect to the section eventually worked out.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
WIBm, 2013-11-19
@core1024

The apache documentation ( http://httpd.apache.org/docs/2.2/mod/mod_alias.html#Redirect ) describes this case in detail:

Then any request beginning with URL-path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-path will be appended to the target URL.
Example:
Redirect /service http://foo2.example.com/service
If the client requests http://example.com/service/foo.txt, it will be told to access http://foo2.example.com/ service/foo.txt instead. Only complete path segments are matched, so the above example would not match a request for http://example.com/servicefoo.txt. For more complex matching using regular expressions, see the RedirectMatch directive.

If a card for an unknown product (example.com/catalog/1234/9999) is requested, then apache takes a suitable match on the left side of the URL path (/catalog/1234/) and redirects it to example.com/section/subsection/subsection_double_name/, with this by appending the rest of the initial URL (9999) at the end.
To avoid this behavior, you should use RedirectMatch or RewriteRule instead of Redirect. Try this:
RedirectMatch ^/catalog/1234/9876/?$ http://example.com/section/subsection/item_name
RedirectMatch ^/catalog/1234/?$ http://example.com/section/subsection/subsection_double_name
Yes, and put down a 301 code (for example, "RedirectMatch 301 ^/catalog/...") for a permanent redirect, so that search engines know that the requested page has permanently moved to another location.
Finally add
RewriteEngine On
RewriteRule ^catalog/([0-9]{4})/[0-9]{4}/?$ /catalog/$1 [L]

S
Stepan, 2013-11-18
@L3n1n

Arrange the redirects in the correct order, and you don't need to cut anything.
In your example, redirect the catalog first, then the product.

W
WIBm, 2013-11-18
@WIBm

After all redirects, add the following (mod_rewrite must be enabled):
RewriteEngine On
RewriteRule ^catalog/([0-9]{4})/[0-9]{4}/?$ /catalog/$1 [L]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question