开发者

how to redirect to another page if file exist in root?

开发者 https://www.devze.com 2023-03-23 13:33 出处:网络
I\'m trying to make a script to put on all my pages on my site saying if so and so file exist in the root directory (\"/\") it will auto redirect to it and if the file isn\'t there it does nothing.

I'm trying to make a script to put on all my pages on my site saying if so and so file exist in the root directory ("/") it will auto redirect to it and if the file isn't there it does nothing.

I'm using this so i can set up a maintenance mode for the site so i can take it down while im working on it. I already have made the maintenance page, I just don't know how to set up the script. The file name is maintenance.html and I only want it to be in the root file. I don't wa开发者_JAVA技巧nt to have to upload it to every directory to take the site down.

The file url would be http://domain.tld/maintenance.html and the script would go if the file is there and redirect to that file else if it's not there don't redirect. I know the redirect code is (in HTML)

<meta HTTP-EQUIV="REFRESH" content="0; url=http://domain.tld">


You should try this in your .htaccess file:

RedirectPermanent / /maintenance.html
RedirectPermanent /page2.html /maintenance.html
RedirectPermanent /anotherpage.html /maintenance.html

And so on. So just do this for each page of your site, on a new line for each.

This will redirect each of your pages right away to the maintenance page.

.htaccess is the best way to do it in my opinion. (better than JavaScript)

Hope this helps.

EDIT:

To use it, first you put:

RedirectPermanent

And then a space and then the page you want to redirect to the maintenance page:

/page.html

And then another space and then the page you want to redirect to:

/maintenance.html

So, all together, here's an example:

RedirectPermanent /page.html /maintenance.html

Note the space in between RedirectPermanent, the page redirecting from and the page redirecting to.

The way it works, well I don't know. This isn't a script, it's a .htaccess file code.


You can use:

if(file_exists('/file.php'))
{
   //do something if file exists
}


The better way would be the put a .htaccess named file in your root folder with the following content:

ErrorDocument 404 /maintenance.html

This redirects automatically to this page, if the called page is not existing.


A set of redirection rules for your webserver is what you need, methinks. If you're running Apache, mod_rewrite is the magic word, if you're running something else, well, then, I wouldn't know the magic word, but something similar exists for most servers, if not all.

But, using Apache's brilliant mod_rewrite, to redirect ALL traffic to a set page or address, e.g. during maintenance, is as simple as:

<IfModule mod_rewrite.c>

# Use mod_rewrite
RewriteEngine on

# If you want, you can exclude yourself by adding a condition for the redirection,
# i.e. if the RewriteCond matches, proceed with the RewriteRule
# This statement checks that the IP of the client isn't 123.456.789.012
RewriteCond %{REMOTE_ADDR} !123.456.789.012

# Redirect all traffic to /maintenance.html with a "307 Temporary Redirect",
# except traffic to the maintenance page.
RewriteCond %{REQUEST_FILENAME} !maintenance.html
RewriteRule .* /maintenance.html [R=307,L]

</IfModule>

Where should these instructions be, you ask? Well, since it's a temporary thing, the most logical would be in a .htaccess file in your webroot. But it's also possible to include the same in your servers/virtualhosts global configuration, which for a permanent ruleset would make sense from an optimization aspect.

To disable the redirection, it's enough to comment out either the RewriteEngine on statement, or the RedirectRule statement. You could also rename your .htaccess to something else or delete it.


It's not very efficient to write your own server-side script to check for a file when your webserver can do it for you. Use Apache's mod_rewrite capability in an .htaccess file; you'll enable (i.e. uncomment) your rewrite rules when you want to put your page in maintenance mode. Doing it this way would also allow you to access the website while you work on it if you put in a rule to allow access from your own IP.

If this is free hosting -- which it seems like it is -- then you may not be able to do this, but I don't see why it would be a major issue to do it. Most webserver software has some sort of rewrite function, and this is a fairly trivial rewrite.

Alternatively you could use a quick-and-dirty bit of Javascript similar to this (might not be exactly this): <script type="text/javascript">location = www.yoursite.com/maintenance.html;</script>

It'd be better to use rewrites, though.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号