Mobile app version of vmapp.org
Login or Join
Becky754

: Redirect old url with several dirs to first level I've a lot of url with same estructure /blog/<category>/item/<slug-article> Such /blog/administracion-sistemas-unix/item/named-run-gigante

@Becky754

Posted in: #Htaccess #UrlRewriting

I've a lot of url with same estructure

/blog/<category>/item/<slug-article>


Such

/blog/administracion-sistemas-unix/item/named-run-gigante


I like create a rule to move a first level
<site>/<slug-article> https://<site>/named-run-gigante


I not found any solution for this without create a rule for every category

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Becky754

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

RedirectMatch 301 /blog/(.*)/item/(.*) /



You seem to be along the right lines, however, this potentially matches "too much" (eg. multiple path segments, any characters, no slug at all, etc.). Generally, you should be as specific as possible. For instance, if your "category" and "slug-article" consist of only lowercase letters and hyphens then try something like:

RedirectMatch 301 ^/blog/[a-z-]+/item/(.+) /


This includes a start of string anchor (^) so /blog/... must occur only at the start of the URL. Also removed the capturing group from the first subpattern - this is unnecessary since you only appear to need the "slug-article".

You'll need to clear your browser cache before testing.



Note that if you have existing mod_rewrite directives (ie. you are using WordPress or other popular CMS) then you should use mod_rewrite instead, to avoid potential conflicts. For example, at the start of your .htaccess file:

RewriteRule ^blog/[a-z-]+/item/(.+) / [R=301,L]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme