1

I am trying to achieve the following:

This URL: www.example.com/wis/9-3/1998/0.general

Redirected to this URL: www.example.com/9440/wis/9-3/1998/0.general

So I'm trying to add this subdirectory to the target URL: /9400/

Also, /0.general changes, so that needs to be obtained from the original URL.

This is what I currently have, but it doesn't work:

RewriteRule ^/wis/9-3/1998/(.*)$ /9400/wis/9-3/1998/$1 [L]
1
  • 1
    /0.general changes <- Is this a subdirectory or file, and does the format for the name remain consistent (e.g., a digit followed by a period and then a word)?
    – dan
    Commented Mar 6, 2019 at 4:44

1 Answer 1

2
RewriteRule ^/wis/9-3/1998/(.*)$ /9400/wis/9-3/1998/$1 [L]

This won't work in a directory (or .htaccess) context because of the slash prefix on the RewriteRule pattern. In .htaccess only a partial URL-path is matched, which notably does not start with a slash. So, this should be written:

RewriteRule ^wis/9-3/1998/(.*) /9400/wis/9-3/1998/$1 [L]

(The end $ is superfluous since regex is greedy by default.)

Note also that this is an internal rewrite, not an external "redirect", as you've stated in your question. (Although an internal rewrite might well be what you require?)

Note also, @dan's comment above. Generally, the regex should be as specific as possible.

You could also "simplify" the above directive to save repetition:

RewriteRule ^(wis/9-3/1998/.*) /9400/$1 [L]

UPDATE#1: Within my htaccess I have some default wordpress expressions

If this is WordPress the you can't perform this type of internal rewrite in .htaccess - because of the way WordPress routes the URLs (it looks at the requested URL, to the rewritten URL).

However, you can use an external redirect (ie. the URL changes). In which, case this should go at the top of your .htaccess file, before the WordPress front-controller:

RewriteRule ^(wis/9-3/1998/.*) /9400/$1 [R,L]

Note the addition of the R flag. (This is a temporary 302 redirect.)

UPDATE#2: is there a way to include a range where 1998 is located. Let's say from 1998 to 2005

Regex is not very good for doing "ranges", however, since you have a fairly limited range you can use alternation and include each possible value: 1998, 1999, 2000, etc.:

RewriteRule ^(wis/9-3/(?:1998|1999|2000|2001|2003|2003|2004|2005)/.*) /9400/$1 [R,L]
6
  • I tried out both of above solutions and it didn't work. Within my htaccess I have some default wordpress expressions and I start the file with RewriteEngine On
    – jcmaad
    Commented Mar 6, 2019 at 16:20
  • "I have some default wordpress expressions" - Ah, if this is WordPress then you can't use .htaccess to rewrite the URL in this way (because of the way WP routes URLs) - you need to do this in WordPress itself. However, you can use a redirect - is a redirect what you require? I'll update my answer...
    – MrWhite
    Commented Mar 6, 2019 at 16:26
  • After reading up on 301 vs 302, I rather have 301 since the redirect is permanent. Do I amend your answer to RewriteRule ^(wis/9-3/1998/.*) /9400/$1 [R=301,L] ?
    – jcmaad
    Commented Mar 6, 2019 at 16:42
  • Yes, that's correct. But you should always test with a temporary redirect first to avoid caching issues.
    – MrWhite
    Commented Mar 6, 2019 at 16:44
  • excellent, thank you. is there a way to include a range where 1998 is located. Let's say from 1998 to 2005.
    – jcmaad
    Commented Mar 6, 2019 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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