homepage Welcome to WebmasterWorld Guest from 54.227.41.242
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
htaccess help
detect URL format and remove from URL
raymondcc




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

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]

 

lucy24




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved