2

How can I redirect:

http://example.com/subdir/a.aspx
http://example.com/subdir/b.aspx
http://example.com/subdir/c.aspx
...

to:

http://example.com/subdir/

with .htaccess please?

Note, I have these on my new site:

http://example.com/subdir/a/
http://example.com/subdir/b/
http://example.com/subdir/c/
http://example.com/subdir/1/
http://example.com/subdir/2/
http://example.com/subdir/3/
...

and they must not be affected.

Edit:

/a.aspx and /a/ are just examples, and not related.

  • Presumably /a.aspx and /a/ are not related? Do you currently have one or more .htaccess files? Do you have any existing directives in your .htaccess file(s)? – MrWhite Sep 27 at 16:28
  • @MrWhite Edited above, and there is just one .htaccess, some existing rules like Redirect /contact.aspx /offices/ plus some WP generated lines. – Stickers Sep 27 at 17:03
1

...some existing rules like Redirect /contact.aspx /offices/ plus some WP generated lines.

Since this is a WordPress site, that is presumably already using mod_rewrite as part of the "front-controller" then you should avoid mixing in mod_alias (Redirect and RedirectMatch) redirect directives. Since the two modules run at different times during the request, despite their apparent order in the config file (mod_rewrite runs first) then you can get some unexpected conflicts. At the very least, it is inefficient, since the URL gets rewritten before the mod_alias Redirect occurs.

Any mod_alias Redirect directives should be converted to the "equivalent" mod_rewrite RewriteRule and placed before the existing WordPress directives. For example, in the case of the above "temporary" redirect from /contact.aspx to /offices/, this would be written as:

RewriteRule ^contact\.aspx$ /offices/ [R=302,L]

Note the absent slash prefix on the RewriteRule pattern.

If this is intended to be permanent, then change the 302 (temporary) to 301 (permanent) but only once you have confirmed that all is working OK. (Note that the Redirect directive you posted in the comment is a temporary-302 redirect.)

Then, to redirect all requests for .aspx files in the /subdir directory to the subdirectory root, ie. /subdir/, then you would do the following:

RewriteRule ^(subdir/)[^/]+\.aspx$ /$1 [R=302,L]

(subdir/) - This captures the name of the subdirectory (to save repetition) and is referenced with the $1 backreference in the substitution.

[^/]+ - This matches 1 or more non-/ characters. So, any filename, but not a URl-path that contains multiple path segments. ie. It won't match /subdir/foo/a.aspx.

I should just add, that from an SEO standpoint, redirecting en masse to a common target like this is a bit dubious. It will likely to be seen as a soft-404 in the eyes of Google. And could be confusing for users, unless the target page is in someway related or contains relevant information for the user.

  • Works well. But I had to put it above the WP rules. Is that ok? – Stickers Sep 29 at 0:44
  • Yes, that is correct, as I mentioned above, "...and placed before the existing WordPress directives." If they are placed after the WP rules and these .aspx files don't exist then the redirects would not even get processed, since the request would be routed through WordPress first (which is the purpose of the WP rules - the "front-controller"). – MrWhite Sep 29 at 9:39
  • Re "...this is a bit dubious..." I know that too. I don't have a better choice. Previously, it was /subdir/doe-a-john.aspx. New url becomes to /subdir/john-a-doe/, and some don't follow the exact reversed last-m-first name pattern. So that I just redirect them all to the index/listing page. – Stickers Sep 29 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.