homepage Welcome to WebmasterWorld Guest from 54.198.135.190
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / WordPress
Forum Library, Charter, Moderators: lorax & rogerd

WordPress Forum

    
Fix Mod Rewrite URL change
ispreview




msg:4441490
 2:31 pm on Apr 16, 2012 (gmt 0)

For part of March 2012 I ran my wordpress install with URLs like these:

http://www.mysite.co.uk/2012/03/cityfibre-uk-agrees-significant-metro-fibre-broadband-investment-with-citigroup/

http://www.mysite.co.uk/2012/03/cat-jumps-off-roof/

But for various reasons I later had to adjust it so that now my URLs look like this.

http://www.mysite.co.uk/index.php/2012/03/cityfibre-uk-agrees-significant-metro-fibre-broadband-investment-with-citigroup/

http://www.mysite.co.uk/index.php/2012/03/cat-jumps-off-roof/

So what I want to do is perform an automatic redirect on any requests for "http://www.mysite.co.uk/2012/03/" (specifically the /2012/03/ part) so that those would then have the /index.php bit added in front. How best to do this without screwing up the rest of my site and should I use [R=301,L] instead of just [L]?

 

lucy24




msg:4441518
 3:31 pm on Apr 16, 2012 (gmt 0)

[R=301,L] is a Redirect. Browser's address bar changes; search engine notes the new URL.

[L] is a Rewrite. Browser's address bar does not change; search engine thinks you are still at the originally requested URL.

Do you want the same page to have two different URLs? (That was a rhetorical question.)

As long as you're redirecting, it would make vastly more sense to redirect from the longer URL to the shorter one. Especially when the longer one has "index.php" smack dab in the middle. Do a Forums search for "rewrite" + "redirect" + "boilerplate". Skip over all posts from me saying "I think there's some boilerplate around here somewhere" ;)

g1smd




msg:4441520
 3:33 pm on Apr 16, 2012 (gmt 0)

The target URL should include the protocol and domain name.

Yes, you need the [R=301,L] flag to make it a 301 redirect, otherwise you'll get a 302 redirect.

It's one line of mod_rewrite code, i.e. a RewriteRule. Let's see your best shot.


However, do note that adding index.php to the URLs that people see and use out on the web is somewhat a backwards step.


Hmm. This is becoming a habit. Must type faster. :)

ispreview




msg:4441884
 10:21 am on Apr 17, 2012 (gmt 0)

My best stabs so far are...

RewriteRule ^index.php/2012/30/([^/\.]+)?$ 2012/03/$1 [R=301,L]

or

RewriteRule ^/index.php/2012/30/$ /2012/30/$1 [R=301,L]

They're probably both wrong in some way, I haven't tried either yet because one mistake could damage a lot of other sites so I'd rather be correct first time.

PS - Yes I know index.php/ is a backwards step but for various reasons I couldn't use the normal WordPress method and had to go with a custom. It actually worked out quite well, if a bit uglier ;) .

ispreview




msg:4442541
 3:56 pm on Apr 18, 2012 (gmt 0)

So am I close :) ?

lucy24




msg:4442685
 10:22 pm on Apr 18, 2012 (gmt 0)

Your rules as written redirect from the longer URL to the shorter one. Is that what you decided to do after all?

All Redirects should give the complete protocol and domain name:

... http://www.example.com/{target here} [R=301,L]

Stepping back... Remember, your users' address bar doesn't have to show the page's "real" address. You can have an address bar saying

www.example.com/foobar

serving content from

www.example.com/index.php?first=f&second=o&third=o&fourth=b&fifth=a&sixth=r

If you want to test a rule without making your whole site explode, simply add a RewriteCond that constrains the rule to yourself. Use your IP address-- or your UA if it's an unusual one. (I generally say "{HTTP_USER_AGENT} Camino", no anchors, which works a treat and doesn't change if I have to restart the modem.)

Don't just test on requests that are supposed to work. Also test requests that are supposed to not work-- and throw in some garbage requests while you're at it.

g1smd




msg:4443247
 3:16 am on Apr 20, 2012 (gmt 0)

Close? Sort of.

Escape literal periods in patterns.

Do not escape slashes.

Specify protocol and domain name in rule target.

Pattern beginning ^index would be used in htaccess and ^/index in httpd.conf

In the second rule, you didn't capture anything, so $1 would be empty.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / WordPress
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