homepage Welcome to WebmasterWorld Guest from 54.197.111.87
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

    
Redirect directory excluding index
all files except index
gniver




msg:4607851
 3:04 pm on Sep 6, 2013 (gmt 0)

The directory that I want to redirect is
example.com/blog/

This is also the index page. I want all files in the directory except for the index page to redirect to /blog2/

Example:
example.com/blog/ --> example.com/blog/
example.com/blog/post1/ --> example.com/blog2/post1/
example.com/blog/post2/ --> example.com/blog2/post2/

I've been trying without success. Thank you.

 

lucy24




msg:4607960
 9:04 pm on Sep 6, 2013 (gmt 0)

C'mon, give us a hint. What have you tried?

The basic pattern would be
RewriteRule ^blog/(.+)
with preceding
RewriteCond %{REQUEST_URI} !^/blog/index

By expressing the capture as .+ rather than .* you've excluded requests for
/blog/ and-that's-all
but you still need to exclude /index (add extension if you like, but it isn't necessary) because your generic "index.html" redirect comes later. You can add an [NS] flag if you like, but I don't think it will affect rule execution. At most it would shave some nanoseconds if /blog/ itself is a real, physical directory-- which I kinda think it isn't.

Are all your pages really disguised as directories (trailing slash)? I assume that's a CMS at work. Does the /blog/ directory contain any non-page files? If so, what should happen to them?

JD_Toims




msg:4608034
 5:42 am on Sep 7, 2013 (gmt 0)

RewriteCond %{REQUEST_URI} !^/blog/(index)?
RewriteRule ^blog/(.+) http://www.example.com/blog2/$1 [R=301,L]

[Yeah, I just felt like coding for a minute -- And, yes, I know it's crazy to just feel the need to write some mod_rewrite occasionally, but I've learned to live with it lol]

lucy24




msg:4608048
 8:32 am on Sep 7, 2013 (gmt 0)

(index)?

Whoops! Remember, the whole point of .+ rather than .* is to exclude requests for /blog/ alone. So there will always be something after it.

The open-ended form
/blog/(index)?
would be slightly disastrous since it effectively excludes all requests :) ("Apply this rule to requests for /blog/ that are not requests for /blog/")

But I have a lurking suspicion that your cat played some role in the post, and the above wasn't really what you meant to say.

gniver




msg:4608052
 8:51 am on Sep 7, 2013 (gmt 0)

Thank you Lucy and JD. Very instructive! I'll give this a try in a couple of hours.

Lucy - you are correct that this is a CMS - Expression Engine, pages disguised as directories. There are no non-page files in the /blog/ directory.

JD_Toims




msg:4608086
 3:06 pm on Sep 7, 2013 (gmt 0)

But I have a lurking suspicion that your cat played some role in the post, and the above wasn't really what you meant to say.

Yeah, there I went coding off the top of my head again and forgot the end anchor. [Grrr! Stupid cat! Bad cat! lol]

RewriteCond %{REQUEST_URI} !^/blog/index\..{3,4}$
RewriteRule ^blog/(.+) http://www.example.com/blog2/$1 [R=301,L]

I did use .+ in the rule purposely just to exclude blog/ a bit quicker, even though I made the index optional in the condition, but the implicit "and everything else" from no end anchor in the condition does really mess things up, so I made a few adjustments that should make it much closer to working and possibly a blip more efficient.

gniver




msg:4608535
 6:52 pm on Sep 9, 2013 (gmt 0)

I tried it out this morning and it isn't working. I probably missed something. The url remains the same - /blog/file_name remains /blog/file_name.

Here's my code:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/blog/index
RewriteRule ^blog/(.+) http://www.example.com/blog2/$1 [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/articles/index
RewriteRule ^articles/(.+) http://www.example.com/articles2/$1 [R=301,L]

lucy24




msg:4608572
 10:29 pm on Sep 9, 2013 (gmt 0)

You only need to say "RewriteEngine on" once per htaccess. (Plus once per <Files> envelope ... but you're not actually supposed to do this, so never mind.) I don't think repetition will actually break anything-- if it did, you'd be seeing 500-class errors-- but it's still a mistake.

There's certainly nothing wrong with the rules as written.

Are there other RewriteRules in the same htaccess that do work? If you make a rule like

RewriteRule foobar\.html http://www.example.com/widget.html [R=301,L]

does it work? (Note that it doesn't matter that you have no files with these names. The browser's address bar will still show the target filename if the redirect happened as intended.) Technically you're supposed to have a line

Options +FollowSymLinks

but in practice any host that allows full htaccess will have this setting in the config file, so it's very unlikely to make a difference. This is assuming for the sake of discussion that your host does allow full-spectrum htaccess. That's why we need to start by making sure you're able to make at least one working RewriteRule-- somehow, somewhere, details don't matter.

Once that's out of the way we move to Consideration #2: Are you using a CMS that's built around rewriting, so there may be a collision with other existing rules?

Oh, and Consideration #1b: Do you have more than one htaccess file on the same path, both using RewriteRules?

JD_Toims




msg:4608581
 11:39 pm on Sep 9, 2013 (gmt 0)

I tried it out this morning and it isn't working. I probably missed something. The url remains the same - /blog/file_name remains /blog/file_name.

What Lucy24 said + make sure you empty your cache before each and every .htaccess Rewrite or Redirect test.

g1smd




msg:4608590
 12:41 am on Sep 10, 2013 (gmt 0)

Where is this code located?

It should be in the file that would logically be found at
example.com/.htaccess if it were web accessible.
gniver




msg:4608596
 12:59 am on Sep 10, 2013 (gmt 0)

It should be in the file that would logically be found at example.com/.htaccess


Yes, g1smd,, that is correct.

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