Welcome to WebmasterWorld Guest from 54.166.117.130

Forum Moderators: Ocean10000 & incrediBILL & phranque

htaccess help

detect URL format and remove from URL

   
4:06 pm on May 10, 2012 (gmt 0)

5+ Year Member



I would like to permanently redirect the below URL:

http://www.example.com/blog/archives/#*$!X/YY/%postname%/

to

http://www.example.com/blog/#*$!X/YY/%postname%/

#*$!X and YY are all numbers.
The four Xs are actually year and the 2 YYs are month.

I tried the below rewrite rule and it don't work. Any help is much appreciated.

RewriteEngine On
RewriteBase /blog/
RewriteRule ^archives/([0-9]+)/([0-9]+)/ /blog/$3 [R=301,NC,L]

[edited by: incrediBILL at 4:59 pm (utc) on May 10, 2012]
[edit reason] fixed URLS, use Example.com [/edit]

8:36 pm on May 10, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Are you rewriting

www.example.com/archives/etc.

to

www.example.com/blog/blog/{nothing}

?

You have "blog" twice, and there is no capture $3. (Um, Bill, you didn't accidentally delete a couple of parentheses did you?)

The RewriteBase declaration refers to the target, not the pattern. But it's irrelevant here because you are Redirecting, not Rewriting, so you should give the complete protocol-plus domain in your Target.

Be as specific as possible. If there are exactly four digits followed by exactly two digits, say so in the Rule:

\d\d\d\d/\d\d
or
[0-9]{4}/[0-9]{2}

are two possible formats. You can capture the whole thing together, since you'll be reusing it in its original form.

Now here is the problem. You are using mod_alias syntax, where the rest of the URL is reattached to the end of both pattern and target. mod_rewrite doesn't work that way; it does a complete replacement. So if there is anything after /archives/ that you want to keep and reuse, you have to capture it, as in

^archives/(\d\d\d\d/\d\d/morestuffhere)

Again, anything that's being reused in the original order can be captured as a package.
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month