RewriteRule - section and subsection

Viewed 18

I need to redirect all subsections to the new URL as rewrite rule in .htaccess.

example:

  • example.com/section => example.com/new_section

  • example.com/section/subsection1 => example.com/new_section

  • example.com/section/subsection1/subsubsection => example.com/new_section

etc (every section has many subsections and rule form all section/subsection there are hundreds of rules)

I have rewrite:

RewriteRule ^section[/]?$ /new_section [R=301,L]

This redirect only URL www.example.com/section to www.example.com/new_section, but not redirect www.example.com/section/subsection1 etc.

1 Answers
RewriteRule ^section[/]?$ /new_section [R=301,L]

The regex ^section[/]?$ only matches section or section/. You need to match section/<anything> as well. For example:

RewriteRule ^section($|/) /new_section [R=301,L]

NB: Test first with a 302 (temporary) redirect to avoid potential caching issues.

and rule form all section/subsection there are hundreds of rules

Not sure exactly what you mean by this, but the order of rules can be important. Generally, the more specific rules need to go first.

Related