1

I had applications running on the following variable links:

www.example.com/topics/vardir/show
www.example.com/education/topics/vardir/vardir/show
www.example.com/literacy/topics/vardir/show
www.example.com/literacy/topics/vardir/vardir/show

I have had to move these to a subdomain:

www.subdomain.example.com/topics/vardir/show
www.subdomain.example.com/education/topics/vardir/vardir/show
www.subdomain.example.com/literacy/topics/vardir/show
www.subdomain.example.com/literacy/topics/vardir/vardir/show

The www.example.com site is now a Wordpress site, running Lightsail Bitnami.

I would like to update www.example.com vhost file with a rewrite rule so people accessing the old links are redirected to the new subdomain.

I already have a rewrite rule working for a variable link with ?elem=icon_file969 at the end which redirects to a subdomain:

www.domainname.com/education/topics/vardir/vardir/show?elem=icon_file969


 RewriteCond %{QUERY_STRING} (^|&)elem=[^&]+(&|$) [NC]
 RewriteCond %{HTTP_HOST} ^(?:www\.)?domainname\.com$ [NC]
 RewriteRule ^/?[^/]+/[^/]+/[^/]+/[^/]+/show/?$ https://subdomain.domainname.com%{REQUEST
_URI} [R=301,L,NC,NE]

Any help would be appreciated.

6
  • I've got this working for another query I needed, I'm just struggling with the question above - RewriteCond %{QUERY_STRING} (^|&)elem=[^&]+(&|$) [NC] RewriteCond %{HTTP_HOST} ^(?:www\.)?domainname\.com$ [NC] RewriteRule ^/?[^/]+/[^/]+/[^/]+/[^/]+/show/?$ subdomain.domainname.com%{REQUEST _URI} [R=301,L,NC,NE] Nov 8, 2023 at 9:08
  • You should update/edit your question to add additional information. Unformatted code in comments is impossible to read (characters are missing due to the formatting). However, that is a "redirect", not a "rewrite" (that you are asking for in the question)? Is that what you require? But what you are asking is then much simpler than the rule you've posted... but what parts are variable in the question? That just looks like 4 static redirects?
    – MrWhite
    Nov 8, 2023 at 9:33
  • Never use a 301 until you've get everything sorted and stable with a 302
    – symcbean
    Nov 8, 2023 at 10:26
  • Thanks, i have updated the question. They are not simply 4 static redirects, as each one has 1 or 2 variable directories. Nov 8, 2023 at 13:01
  • So, from your edit, I assume vardir is a "variable directory" (a single path segment) and the rest is static? And by being variable, I assume letters a-z only? Or alphanumeric? Including hyphens? Underscores?
    – MrWhite
    Nov 8, 2023 at 16:01

1 Answer 1

0

From comments, vardir is a variable directory name that can contain alphanumeric characters and underscores. The other path segments are static. Since you are wanting to "redirect" (not "rewrite" as stated initially) to the same URL-path as requested then you can do this with a single rule.

For example:

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{REQUEST_URI} ^/topics/\w+/show$ [OR]
RewriteCond %{REQUEST_URI} ^/education/topics/\w+/\w+/show$ [OR]
RewriteCond %{REQUEST_URI} ^/literacy/topics/\w+(/\w+)?/show$
RewriteRule ^/?((education|literacy)/)?topics/\w+(/\w+)?/show$ https://www.subdomain.example.com%{REQUEST_URI} [R=301,L]

The \w shorthand character class is the same as [a-zA-Z0-9_] (alphanumeric mixed case and underscores).

Any query string on the initial request is passed through unchanged.

The RewriteRule pattern is a more general regex and matches all URLs that need to redirect plus many more that don't. The preceding RewriteCond directives then narrows the selection down to just those that should redirect.

1
  • 1
    Thanks for this. The single rule makes sense. I've just tried it and it's working. Nov 9, 2023 at 13:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .