Forums | MacLife
You are not logged in.
#1 2008-09-28 8:58 pm
Mod_rewrite / Regex question
Say I wanted /blog/(whatever) to redirect to /(whatever). I'm having trouble laying hold of the intricacies of the syntax. So far I've tried:
Code:
RewriteRule ^blog/[.*]$ /$1 [R]
And a few variations on that. But obviously that's wrong, since it's not working. What would be the proper syntax here, to do what I need it to?
Offline
#2 2008-09-28 10:06 pm
Re: Mod_rewrite / Regex question
Wouldn't it be
Code:
RewriteRule ^blog/(.*) /$1 [R=301,L]
?
So, just when did this place get Private Messages? YIKES!
Gippy Pages | Fuzzy Coconut XHTML Widget | PuppyCam
Offline
#3 2008-09-28 10:53 pm
Re: Mod_rewrite / Regex question
Still not working (as is, or with a $ to terminate the first condition) - it still returns a 404. Good call with the 301, though. That hadn't crossed my mind.
Last edited by Thrica (2008-09-28 10:57 pm)
Offline
#4 2008-09-29 12:32 am
Offline
#5 2008-09-29 12:42 am
Re: Mod_rewrite / Regex question
Still 404ing with no redirect.
I don't know if the Wordpress rules could be causing a conflict - here's my .htaccess, if it helps:
Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^blog/?(.*)$ /$1 [R=301,NC]
ErrorDocument 404 /404.html
</IfModule>Offline
#6 2008-09-29 1:02 am
Re: Mod_rewrite / Regex question
[L] means last. It's the equivalent of saying "Don't follow rules coming after this one."
How about
Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
RewriteRule ^blog/?(.*)$ /$1 [R=301,NC,L]
ErrorDocument 404 /404.html
</IfModule>Offline
#7 2008-09-29 9:20 pm
Re: Mod_rewrite / Regex question
Put blog rule before the index.php rule.
Those first wordpress rules are essentially saying that if the file isn't real, send it to index.php. Your 2nd rule probably isn't being applied.
So, just when did this place get Private Messages? YIKES!
Gippy Pages | Fuzzy Coconut XHTML Widget | PuppyCam
Offline
#8 2008-09-30 7:35 pm
Re: Mod_rewrite / Regex question
Ok, it's still not working, but I've figured out it's not necessarily a problem with the syntax: If I put in rules that I know worked before next to the blog rule, they don't work either.
Code:
RewriteEngine On
RewriteRule ^blog/(.*)$ /$1 [R=301,NC]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
ErrorDocument 404 /404.htmlThe whole issue was brought about when I moved Wordpress from /blog/ to the root directory, so all the Wordpress junk that used to be in the .htaccess of /blog/ is now in the root .htaccess.
Could the latter rules still be overriding the previous ones?
Offline
#9 2008-09-30 9:15 pm
Re: Mod_rewrite / Regex question
Have you edited wordpress' admin page that asks you where wordpress lives?
So, just when did this place get Private Messages? YIKES!
Gippy Pages | Fuzzy Coconut XHTML Widget | PuppyCam
Offline
#10 2008-10-01 11:59 am
Offline
#11 2008-10-01 11:33 pm
Re: Mod_rewrite / Regex question
Pending that it was set up correctly and that your theme properly used bloginfo(), get_permalink() and the like you shouldn't have an issue moving a blog down one directory... WordPress should take care of that by itself.
Last edited by Gipetto (2008-10-01 11:35 pm)
So, just when did this place get Private Messages? YIKES!
Gippy Pages | Fuzzy Coconut XHTML Widget | PuppyCam
Offline
#12 2008-10-02 1:29 am
Re: Mod_rewrite / Regex question
Are you hosting this on your own OS X machine? Because that won't even look at .htaccess files by default.
Offline
#14 2008-10-02 11:07 pm
Re: Mod_rewrite / Regex question
Gipetto wrote:
Pending that it was set up correctly and that your theme properly used bloginfo(), get_permalink() and the like you shouldn't have an issue moving a blog down one directory... WordPress should take care of that by itself.
The blog itself works fine; I just want the old permalinks to redirect to the new permalinks.
It's on a remote BSD box (nearlyfreespeech.net, FWIW)
According to rewritelog (temporarily set to high), this is what happens when I visit http://www.thrica.com/blog/archives/223 - which should redirect to http://www.thrica.com/archives/223, but instead returns a 404. Pretend my IP and the timestamp are before each line; I removed those.
Code:
[thrica.nfshost.com/sid#cbd018][rid#b0e060/initial] (3) [per-dir /f1/content/thrica/public/] add path-info postfix: /f1/content/thrica/public/blog -> /f1/content/thrica/public/blog/archives/223 [thrica.nfshost.com/sid#cbd018][rid#b0e060/initial] (3) [per-dir /f1/content/thrica/public/] strip per-dir prefix: /f1/content/thrica/public/blog/archives/223 -> blog/archives/223 [thrica.nfshost.com/sid#cbd018][rid#b0e060/initial] (3) [per-dir /f1/content/thrica/public/] applying pattern '^blog/(.*)$' to uri 'blog/archives/223' [thrica.nfshost.com/sid#cbd018][rid#b0e060/initial] (2) [per-dir /f1/content/thrica/public/] rewrite blog/archives/223 -> /archives/223 [thrica.nfshost.com/sid#cbd018][rid#b0e060/initial] (2) [per-dir /f1/content/thrica/public/] explicitly forcing redirect with http://thrica.nfshost.com/archives/223 [thrica.nfshost.com/sid#cbd018][rid#b0e060/initial] (3) [per-dir /f1/content/thrica/public/] add path-info postfix: http://thrica.nfshost.com/archives/223 -> http://thrica.nfshost.com/archives/223/archives/223 [thrica.nfshost.com/sid#cbd018][rid#b0e060/initial] (3) [per-dir /f1/content/thrica/public/] applying pattern '.' to uri 'http://thrica.nfshost.com/archives/223/archives/223' [thrica.nfshost.com/sid#cbd018][rid#b0e060/initial] (2) [per-dir /f1/content/thrica/public/] rewrite http://thrica.nfshost.com/archives/223/archives/223 -> /index.php [thrica.nfshost.com/sid#cbd018][rid#b0e060/initial] (2) [per-dir /f1/content/thrica/public/] trying to replace prefix /f1/content/thrica/public/ with / [thrica.nfshost.com/sid#cbd018][rid#b0e060/initial] (1) [per-dir /f1/content/thrica/public/] internal redirect with /index.php [INTERNAL REDIRECT]
It looks to me like it's redirecting correctly, but then reduplicating into a different nonexistent URL. Although, I've never read these before so I can't be sure. The URL doesn't change when I visit that site - shouldn't it, because of the R flag?
Last edited by Thrica (2008-10-02 11:15 pm)
Offline
#15 2008-10-03 9:58 am
Re: Mod_rewrite / Regex question
I just messed around with this and I'm having a problem getting WP to behave with pretty permalinks enabled.
So, just when did this place get Private Messages? YIKES!
Gippy Pages | Fuzzy Coconut XHTML Widget | PuppyCam
Offline




