I'm trying to redirect a lot of pages on a website I own. There are a few pages indexed and I need to fix them for example:

  • /vloerafwerking/projecttapijt/raamafwerking/ needs to go to /vloerafwerking/
  • /vloerafwerking/projec4/raamafwerking/ needs to go to /vloerafwerking/

I have some more pages with examples like these, but I think you get the idea. The pages have things behind the slash on the previous website, and they are all gone on the new one now.

I tried the following, but it doesn't work:

RewriteRule ^vloerafwerking/([a-zA-Z0-9])$ /vloerafwerking/$1[R=301]
  • 1
    Every page in a vloerafwerking subfolder should be redirected the main vloerafwerking-folder? – Pit Feb 15 '19 at 9:00

If you are wanting to redirect specific URLs, as in your examples, then you could do something like the following using mod_rewrite at the top of your .htaccess file:

RewriteEngine On

RewriteRule ^(vloerafwerking/)projecttapijt/raamafwerking/ /$1 [R=302,L]
RewriteRule ^(vloerafwerking/)projec4/raamafwerking/ /$1 [R=302,L]
# etc...

These will need to be arranged in order specificity - if you have conflicting directives. The most specific rules should be first.

The $1 backreference is simply to avoid having to repeat the base directory name.

These are 302 (temporary) redirects. Change to a 301 (permanent) redirect - if that is the intention - only after you have confirmed that it works OK. This is to avoid the browser caching erroneous redirects whilst testing.

However, if you are wanting to redirect any URL that simply starts /vloerafwerking/ as suggested by your code sample then you could do the following instead:

RewriteRule ^(vloerafwerking/). /$1 [R=302,L]

The dot (.) at the end of the RewriteRule pattern matches a single character, so it matches /vloerafwerking/<something>, and not /vloerafwerking/ (which would create a redirect loop).

RewriteRule ^vloerafwerking/([a-zA-Z0-9])$ /vloerafwerking/$1[R=301]

There are a number of "errors" with your attempt:

  • ([a-zA-Z0-9])$ - This matches just a single character in the range a-z, A-Z, 0-9 after the directory name. Your example URLs consist of more than 1 character, so you either need to add the + quantifier (1 or more characters) or remove the $ anchor. You also need to match / (slash) - which is missing from the character class.

  • (...) / $1 - Not sure why you are capturing the URL-path you are wanting to remove and using this in the substitution? This is exactly what you don't want to do; you want to remove, not add it back again. (This would also result in a redirect loop.)

  • .../$1[R=301] - You are missing a space delimiter before the flags argument. This would result in a malformed redirect. You need a space: .../$1 [R=301]

  • You are also missing the L flag, so processing would continue through the file. (Unless this is already the last directive.)

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.