Problem with Ignoring a folder in the rewrite

New to mod_rewrite? This is a good place to start.

Problem with Ignoring a folder in the rewrite

Postby kennova » Mon Jun 08, 2009 11:28 am

I have moved my blog from http://blah.com/home/blog to http://blog.blah.com

I created an .htaccess file and used the following redirects
Code: Select all
RewriteEngine On
RewriteBase /
RedirectMatch 301  ^/home/modules/blog(.*)$ http://blog.blah.com$1
RedirectMatch 301  ^/home/blog(.*)$ http://blog.blah.com$1
RedirectMatch 301  ^/blog(.*)$ http://blog.blah.com$1


This works great. However, (you knew it was coming), I have old posts and those posts have images that exist in the folder.

http://www.blah.com/home/modules/blog/uploaded_images/

So I want to exclude the uploaded_images folder. From I could read in the documentation there are a couple ways to do this, a back reference to a parameter using rewritecond and a subsequent rewriterule with a R=301 so I did that. (The other way is with the full http_uri and I tried that too)
Code: Select all
RewriteEngine On
RewriteBase /
RewriteCond $1 !uploaded_images/
RewriteRule  ^/home/modules/blog(.*)$ http://blog.blah.com$1 [L,R=301]
RedirectMatch 301  ^/home/blog(.*)$ http://blog.blah.com$1
RedirectMatch 301  ^/blog(.*)$ http://blog.blah.com$1


The RedirectMatch rules still work, but the new RewriteRule does not. So existing blog posts found at

http://www.blah.com/home/modules/blog/2 ... -post.html

no longer redirect to http://blog.blah.com/2009/06/blog-post.html like they should.

My images don't redirect either, but that's a good thing I guess. If I remove the RewriteCond it doesn't change anything. I think it's something wrong with the rewriterule but I can't figure it out. I've stared at it for 2 days and read everything I could find. HELP!!
kennova
 
Posts: 8
Joined: Mon Jun 08, 2009 11:13 am

Postby richardk » Mon Jun 08, 2009 3:37 pm

I created an .htaccess file and used the following redirects
Code: Select all
RewriteEngine On
RewriteBase /
RedirectMatch 301  ^/home/modules/blog(.*)$ http://blog.blah.com$1
RedirectMatch 301  ^/home/blog(.*)$ http://blog.blah.com$1
RedirectMatch 301  ^/blog(.*)$ http://blog.blah.com$1

This works great. However, (you knew it was coming), I have old posts and those posts have images that exist in the folder.

RedirectMatch is part of mod_alias not mod_rewrite. If you are only using Redirect* directives you do not need
Code: Select all
RewriteEngine On
RewriteBase /


Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond $1 !^/uploaded_images/.+$
RewriteRule ^home/modules/blog(/.+)$ http://blog.blah.com$1 [R=301,L]
RewriteRule ^(home/)?blog(/.+)$      http://blog.blah.com$1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

hmmm

Postby kennova » Mon Jun 08, 2009 4:24 pm

Thanks for the reply. But. that didn't work and now the other redirects that were working no longer work either.

It might be a problem later down the line.

In the "home" directory I have an .htaccess file
Code: Select all
RewriteEngine on
RewriteRule \.(css|jpe?g|gif|png)$ - [L]
RewriteRule ^([^/\.]+)/?$ index.php?module=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?module=$1&location=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?module=$1&location=$2&id=$3 [L]


but that's it, no other folders have any rules. The rules you posted are sitting on the root directory.
kennova
 
Posts: 8
Joined: Mon Jun 08, 2009 11:13 am

I think it's an issue...

Postby kennova » Mon Jun 08, 2009 5:28 pm

I thin k it's still something with the rewriterules in general.
Code: Select all
RewriteRule ^home/modules/blog(/.+)$ http://blog.blah.com$1 [R=301,L]
RewriteRule ^(home/)?blog(/.+)$      http://blog.blah.com$1 [R=301,L]


Even these all by themselves don't work. So even without the conditional I think there is something up with using rewrite rules. The rules in the "home" folder work though so I know I have the server configured properly.
kennova
 
Posts: 8
Joined: Mon Jun 08, 2009 11:13 am

Postby richardk » Tue Jun 09, 2009 3:37 pm

What URL are you visiting to test?

Code: Select all
RewriteRule ^(home/)?blog(/.+)$      http://blog.blah.com$1 [R=301,L]

should be
Code: Select all
RewriteRule ^(home/)?blog(/(.+))?$      http://blog.blah.com$3 [R=301,L]


Try adding
Code: Select all
RewriteOptions Inherit

in /home/.htaccess.

Or try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond $1 !^/uploaded_images/.+$
RewriteRule ^home/modules/blog(/(.+))?$ http://blog.blah.com$2 [R=301,L]
RewriteRule ^(home/)?blog(/(.+))?$      http://blog.blah.com$3 [R=301,L]

RewriteRule ^home/.+\.(css|jpe?g|gif|png)$ - [L]
RewriteRule ^home/([^/\.]+)/?$ index.php?module=$1 [L]
RewriteRule ^home/([^/\.]+)/([^/\.]+)/?$ index.php?module=$1&location=$2 [L]
RewriteRule ^home/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?module=$1&location=$2&id=$3 [L]

in a .htaccess file in your document root and no .htaccess file in /home.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

getting closer

Postby kennova » Tue Jun 09, 2009 5:56 pm

Option 2 or 3 didn't work
Test url
Code: Select all
http://www.blah.com/home/modules/blog/2009/06/apple-day-keeps-doctor-away-right.html


root .htaccess file I have now
Code: Select all
Options +FollowSymLinks
RewriteEngine On
RewriteCond $1 !^/uploaded_images/.+$
RewriteRule ^home/modules/blog(/(.+))?$ http://blog.blah.com$2 [R=301,L]
RewriteRule ^(home/)?blog(/(.+))?$      http://blog.blah.com$3 [R=301,L]


/home .htaccess file
Code: Select all
RewriteEngine on
RewriteRule \.(css|jpe?g|gif|png)$ - [L]
RewriteRule ^([^/\.]+)/?$ index.php?module=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?module=$1&location=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?module=$1&location=$2&id=$3 [L]


This works when I try to to www.blah.com/blog and I get redirected to blog.blah.com

www.blah.com/home/blog - doesn't get redirected
www.blah.com/home/modules/blog - doesn't get redirected.

also the test url does not redirect. So we are getting closer and at least some rewrites are working. I really appreciate the help.
kennova
 
Posts: 8
Joined: Mon Jun 08, 2009 11:13 am

Postby richardk » Wed Jun 10, 2009 1:21 pm

What happens with
Code: Select all
RewriteOptions Inherit

added to /home/.htaccess?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

tried that too

Postby kennova » Wed Jun 10, 2009 5:50 pm

Yeah I tried that too. I also turned on logging, but I couldn't understand what it was telling me.
kennova
 
Posts: 8
Joined: Mon Jun 08, 2009 11:13 am

Postby richardk » Thu Jun 11, 2009 10:40 am

Post any relevant parts of the log.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

turned the log back on

Postby kennova » Fri Jun 19, 2009 9:15 am

I turned the log back on and went to the different locations like

http://www.blah.com/home/blog
http://www.blah.com/home/modules/blog
http://www.blah.com/home/modules/blog/2 ... right.html

and got the following log

Currently I have .htaccess files on
/ (root)
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond $1 !^/uploaded_images/.+$
RewriteRule ^home/modules/blog(/(.+))?$ http://blog.blah.com$2 [R=301,L]
RewriteRule ^(home/)?blog(/(.+))?$      http://blog.blah.com$3 [R=301,L]


/home
Code: Select all
RewriteEngine on

RewriteRule \.(css|jpe?g|gif|png)$ - [L]
RewriteRule ^([^/\.]+)/?$ index.php?module=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?module=$1&location=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?module=$1&location=$2&id=$3 [L]
Last edited by kennova on Fri Jun 19, 2009 11:35 am, edited 1 time in total.
kennova
 
Posts: 8
Joined: Mon Jun 08, 2009 11:13 am

Next

Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 98 guests

cron