Welcome to WebmasterWorld Guest from 54.166.114.43

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirect directory excluding index

all files except index

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

10+ Year Member



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.
9:04 pm on Sep 6, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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?
5:42 am on Sep 7, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month



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]
8:32 am on Sep 7, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



(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.
8:51 am on Sep 7, 2013 (gmt 0)

10+ Year Member



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.
3:06 pm on Sep 7, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month



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.
6:52 pm on Sep 9, 2013 (gmt 0)

10+ Year Member



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]
10:29 pm on Sep 9, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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?
11:39 pm on Sep 9, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month



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.
12:41 am on Sep 10, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.
12:59 am on Sep 10, 2013 (gmt 0)

10+ Year Member



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


Yes, g1smd,, that is correct.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month