Forum Moderators: phranque

Message Too Old, No Replies

htaccess image trouble

Trying to create a temp maintenance page, can't display images

         

Larkin

3:30 am on Nov 27, 2009 (gmt 0)

10+ Year Member



Hi all,

Very new to working with server technologies, but know my way around XHTML and CSS and know enough to cause troubles with PHP, but I'm having a wicked time getting background images to display on a 302 redirect to a temporary page. Here's the code I'm running:

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^11\.111\.111\.111
RewriteCond %{REQUEST_URI} !^/default\.html$
RewriteRule ^(.*)$ http://example.com/default.html [R=302,L]

Styles are in the top of default.html and image paths are correct to my eye.

Bluehost support said the htaccess code I'm running is the problem and I shouldn't be using this method anyway?

Any thoughts or help super appreciated! Wicked glad to have found this place, perfect reading material or those late nights.

~Larkin

[edited by: jdMorgan at 4:30 pm (utc) on Nov. 27, 2009]
[edit reason] example.com [/edit]

jdMorgan

1:54 pm on Nov 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Except for the explicit 'loop prevention' exception, your rule redirects *all* client requests, including those for images...

Refine your definition of which URLs you actually wish to redirect to the maintenance page, and then code that refined solution.

Jim

Larkin

2:09 pm on Nov 27, 2009 (gmt 0)

10+ Year Member



Thanks Jim. How would I set this up to redirect all visitors to the default.html but still allow image requests to go through so they can still see my background images?

Again, thanks for the help.

~Larkin

jdMorgan

2:21 pm on Nov 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're thinking in terms of 'redirecting visitors' and that complicates things.

Think in terms of 'redirecting client requests for specific URLs' to simplify matters, since that is how mod_rewrite makes its decisions...

What URLs do you wish to redirect?
What URLs do you not wish to redirect?

The 'intersection' of the answers to these two questions indicates the most efficient solution.

It could be as simple as saying "I want to redirect all URL requests, except for 'default.html' and any image, CSS, or external JavaScript file URLs" -- but take extreme care here... What about your custom error page URLs? robots.txt file? sitemap.xml? Google/Yahoo/Bing webmaster tools site-validation/verification file URLs?

Solid definition first, then code.

Jim

Larkin

2:36 pm on Nov 27, 2009 (gmt 0)

10+ Year Member



This is where I get lost, because I'm a designer and this type of work is all new to me.

Basically, I want to redirect client requests for [poptoptcreative.com...] and any subpages to [poptopcreative.com...]

But I want to allow my IP through so that I can continue working on the site.

g1smd

2:49 pm on Nov 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, but you need to think about whether you mean "all" requests, or whether that includes or does not include 'default.html' and any image, CSS, or external JavaScript file URLs" -- but take extreme care here... What about your custom error page URLs? robots.txt file? sitemap.xml? Google/Yahoo/Bing webmaster tools site-validation/verification file URLs?

Larkin

3:07 pm on Nov 27, 2009 (gmt 0)

10+ Year Member



Well, as you probably have figured out, this is well beyond my skill level. In my researching this, I had assumed that putting this as a 302 would let these other requests from search engines and such see that it was a temporary redirect, so I figured I'd be good.

My ultimate goal is to make sure normal human visitors trying to view my site see a temporary splash page with my background images. Right now, they get sent to default.html, but no images appear, just text. Is there a rewrite rule or something I can add that allows images to be shown on this default.html page?

jdMorgan

3:53 pm on Nov 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes there is, but we need an iron-clad definition here of what you do and don't want to redirect.

It's often not clear to those just getting into this that one little error, be it a syntax error caused by a typo, or a 'scope' or logic error in the definition of the 'goal' of the rule, can sink your site in the search engines, and if that matters to your bottom line, basically put you out of business.

So bear in mind that .htaccess is a server configuration file and toying with it is ill-advised. This is not a trivial enterprise. You've got to get it absolutely right -- no wiggle room.

Are all of your 'pages' of type .html? Or do you also have .php or .shtml or .htm or .shtm or .xml or .cfm or any other types of 'pages?'

Do you use custom error pages for 404, 410, 403 responses, etc.? If so, what are their URLs?

We're not being pedantic here, we are basically trying to avoid being involved in an "assisted suicide."

Jim

Larkin

4:12 pm on Nov 27, 2009 (gmt 0)

10+ Year Member



Absolutely understand you both are trying to make sure I don't do something that will negatively affect the site without my knowing! A big thanks for the guidance and I take no offense to being pushed for info if that's what's needed to make things right.

This is a WordPress site, so most pages are .php. I just spoke with my hosting service support people, and the gentleman I spoke with put a temporary redirect on the site through the control panel. He then changed htaccess to the following:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REMOTE_HOST} !^11\.11\.111\.111
RewriteRule ^/?$ "http\:\/\/example\.com\/default\.html" [R=302,L]

This appears to be working, meaning I have access to the site, others go to default.html and the page appears as I wanted it to appear.

Not sure if I'm doing any unintentional harm to my site with this, but hopefully not.

Again, thanks to you both for your input, and if this looks like I'm heading down the path of an assisted suicide, let me know!

Hopefully at somepoint by hanging around these forums I'll get a better grasp of this side of the web, as opposed to the pretty part!

~J

[edited by: jdMorgan at 4:28 pm (utc) on Nov. 27, 2009]
[edit reason] example.com [/edit]

g1smd

4:21 pm on Nov 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$

This can be simplified greatly:

RewriteCond %{HTTP_HOST} ^[b]([/b]www[b]\.)?[/b]example[b]\[/b].com$

and the literal periods need to be escaped by adding the \ backslashes as above.

"http[b]\[/b]:[b]\[/b]/[b]\[/b]/example[b]\[/b].com[b]\[/b]/default[b]\[/b].html"

The quotes and all of the \ backslashes should be omitted.

http://example.com/default.html

This part is a literal URL.

Larkin

4:35 pm on Nov 27, 2009 (gmt 0)

10+ Year Member



Thanks g1smd!

Just made those changes so now I have:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteCond %{REMOTE_HOST} !^11\.111\.111\.111
RewriteRule ^/?$ "http://example.com/default.html" [R=302,L]

Appears to be working fine. Thanks for the simplification.

Larkin

[edited by: jdMorgan at 5:00 pm (utc) on Nov. 27, 2009]
[edit reason] example.com [/edit]

jdMorgan

4:59 pm on Nov 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The HTTP_HOST test is only needed if you host a site *other than* your "poptop" site in this same server filespace. Is that the case?

This could indeed still be a case of assisted suicide, because you need to answer all the questions I asked -- at least for yourself and your hosting-company "helper." The 'exclusion list' needs to be thorough, or you could put your server into an 'infinite' redirection loop.

With what we (here) know so far, this would be my best current guess:


RewriteEngine on
# Temporarily (302) redirect all html and php page requests to the "maintenance" page, excluding
# requests for my own IP address, the maintenance page itself, and any custom error pages.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{REMOTE_ADDR} !=11.11.111.111
RewriteCond $1 !^default\.html$
RewriteCond $1 !^custom-400-error-page\.html$
RewriteCond $1 !^custom-403-error-page\.html$
RewriteCond $1 !^custom-404-error-page\.html$
RewriteCond $1 !^custom-405-error-page\.html$
RewriteCond $1 !^custom-408-error-page\.html$
RewriteCond $1 !^custom-409-error-page\.html$
RewriteCond $1 !^custom-410-error-page\.html$
RewriteCond $1 !^custom-411-error-page\.html$
RewriteCond $1 !^custom-412-error-page\.html$
RewriteCond $1 !^custom-413-error-page\.html$
RewriteCond $1 !^custom-414-error-page\.html$
RewriteCond $1 !^custom-415-error-page\.html$
RewriteCond $1 !^custom-500-error-page\.html$
RewriteCond $1 !^custom-501-error-page\.html$
RewriteCond $1 !^custom-503-error-page\.html$
RewriteCond $1 !^custom-505-error-page\.html$
RewriteRule ^(([^/]*/)*([^.]+\.)+(s?html?¦php[0-9]+))?$ http://example.com/default.html [R=302,L]

Obviously, you need to fill in the correct local URL-paths to your custom error pages if you have any of those shown here, and you can delete the lines for those that you don't use. Some optimization of these error-page exclusion lines is certainly possible, but we'd need to know the URL-paths to determine the optimization details.

Note also the several subtle and not-so-subtle changes to the first few RewriteConds -- they're important, as is everything else...

The regex pattern in the RewriteRule is intended to catch all .html and .php page requests, as well as requests for variants on those filetypes, such as .shtml or .htm, and .php4, .php5, etc. Note that the entire pattern match is also optional, which allows it to also match requests for your 'home page' at "example.com/"

Important:
Replace the broken pipe "¦" character in the RewriteRule pattern with a solid pipe character from your keyboard before use; posting on this forum modifies the pipe characters, and the code won't work unless they're replaced.

Be very sure that the hostname in the redirect target address matches the hostname that you are 'known by' in search engine listings -- if most of your inbound links are to 'www.example.com' and that "www" hostname is what shows up in search engine listings for your pages, then you must include that 'www.' in the redirect target address.

OK, so that's all the "speculation time" I have for today -- I hope it helps. :)

Jim

Larkin

6:19 pm on Nov 27, 2009 (gmt 0)

10+ Year Member



Thanks to you both for your help, much appreciated!

Larkin