Forum Moderators: phranque

Message Too Old, No Replies

NEWBIE - helpon rewrite rule to redirect to file

newbie rewrite post

         

smoosh

3:51 pm on Sep 11, 2007 (gmt 0)

10+ Year Member



I hope someone canhelp here... I'm fresh to Apache and learnignthe ropes. I have a login page that, on click of teh login link sends the user to a sub-dir, within which there is a .htaccess fie taht does an auth check.

I want to temporarily move this auth check out of teh way and replace the current .htaccess file with a new one that redirects the user to a specific shtml page while I update my site behind the scenes.

So far I have tried;

RewriteEngine On

RewriteBase /mysite/

RewriteCond ^.*$ !-f
RewriteCond ^.*$ !-d
RewriteRule ^(.*)$ /mysite/site_closed.shtml $1

This doesn't work - I get a server 500 error.

However, if I set the last line to;

RewriteRule ^(.*)$ /mysite/subdir/ $1

it seems to work?

Can anyone help here? Am I missing a key Apache config that is stopping the rewrite rule from redirecting to an .shtml file?

Thanks,

S.

jdMorgan

10:07 pm on Sep 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi smoosh, and welcome to WebmasterWorld!

It appears that you may be trying to "guess" this code, instead of designing it with the documentation [httpd.apache.org] at hand. Here's what your code says:


# If no file named "^.*$" exists
RewriteCond ^.*$ !-f
# and if no directory named "^.*$" exists
RewriteCond ^.*$ !-d
# then rewrite [b]all[/b] requests to /mysite/site_closed.shtml with an invalid RewriteRule 'flag' of $1
RewriteRule ^(.*)$ /mysite/site_closed.shtml $1

Note that this rule will create a loop, because it will rewrite /mysite/site_closed.shtml to /mysite/site_closed.shtml recursively, and cause a server error.

The RewriteConds don't do anything, since the result is always "true" and the rule will run.

What are the requirements, in terms of requested URLs that should be rewritten to the temporary page, and those that should not be rewritten to the temporary page?

Jim

smoosh

8:15 am on Sep 12, 2007 (gmt 0)

10+ Year Member



Thanks for getting back to me - appreciated!

The requirements are;

I have an old dir where there used to be live content. Rather than decommission the dir and sub dirs, as there are likely to be end users who still have legacy URLs to this old content, I want to place a .htaccess redirect to a simple static webpage in another location, in the highest level dir relative to the old content.

I'd also like to, if possible, exclude specific sub-dirs from the redirect. I have found a line of code that will apparently do this;

RewriteRule ^subdir/ - [L]

I'm assuming the first 2 lines of code (the RewriteCond lines) should be removed; from what you say the arguements stated will always be true and so they're pointless?

Big thanks, steep learning curve for me.

Smoosh

jdMorgan

1:48 pm on Sep 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd also like to, if possible, exclude specific sub-dirs from the redirect. I have found a line of code that will apparently do this;

RewriteRule ^subdir/ - [L]

That's one way to do it. It's not widely-useful though, because it tells mod_rewrite to 'quit' right there. Therefore, you can't have any other rules that perform other unrelated functions at any point in the file beyond this rule.

I'm assuming the first 2 lines of code (the RewriteCond lines) should be removed; from what you say the arguments stated will always be true and so they're pointless?

Yes, since they will always resolve as "true" they don't actually do anything. The RewriteCond directive expects either mod_rewrite variables, server variables, or literal text, and not regular-expressions patterns in that field.

Something like this, located in /directory/.htaccess, would probably be simpler to implement and maintain:


# Do not redirect requests for site_closed.html
RewriteCond $1 !^site_closed\.html$
# Do not redirect requests for the following subdirectories
RewriteCond $1 !^retained_subdirectory1/
RewriteCond $1 !^retained_subdirectory2/
RewriteCond $1 !^retained_subdirectory3/
# Externally redirect all other requests to the site_closed page
RewriteRule (.*) http://www.example.com/site_closed.shtml [R=302,L]

This may need some minor tweaking depending on where you want to locate the .htaccess code and where the site_closed page is located.

"!" means NOT, and the RewriteConds are ANDed together by default.

Jim

phranque

8:40 pm on Sep 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, smoosh!

smoosh

11:11 am on Sep 13, 2007 (gmt 0)

10+ Year Member



Big thanks guys, very much appreciate your help; didn't expect such a warm welcome being a fresh fish.

Smoosh

jdMorgan

3:00 pm on Sep 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As were most when we showed up here -- to include your moderator... :)

Jim