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

    
rewrite everything except a single url
glimbeek




msg:4135857
 12:40 pm on May 20, 2010 (gmt 0)

I'm doing the following:

RewriteCond %{HTTP_HOST} ^www.exam-ple.com [OR]
RewriteCond %{HTTP_HOST} ^exam-ple.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Which works fine.

Now I want to exclude a single file for both options:

RewriteCond %{HTTP_HOST} ^www.exam-ple.com [OR]
RewriteCond %{HTTP_HOST} ^exam-ple.com
RewriteCond %{REQUEST_URI} !^/thefile\.html$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


but going to [()exam-ple.com...] still gets redirected to: http://www.example.com/thefile.html

What am doing wrong?

 

glimbeek




msg:4135890
 1:51 pm on May 20, 2010 (gmt 0)

Note:

The file I DO NOT want to redirect is a google verification file, for instance:
google1aad878a5v471m44.html

Using the following code works:
RewriteCond %{HTTP_HOST} ^www.exam-ple.com [OR]
RewriteCond %{HTTP_HOST} ^exam-ple.com
RewriteCond %{REQUEST_URI} !^/thefile/$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

But that's for a folder not for a file... more specifically the Google verification file (:

Creating it for a file, using:

RewriteCond %{REQUEST_URI} !^/testt\.html$ [NC]

Works as well...

So why doesn't it work for the Google verification file?

jdMorgan




msg:4135893
 1:58 pm on May 20, 2010 (gmt 0)

Your code can be simplified and improved, but it should have worked... Did you delete your browser cache before testing?

RewriteCond %{HTTP_HOST} ^(www\.)?exam-ple\.com [NC]
RewriteCond $1 !^excluded-file\.html$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Jim

glimbeek




msg:4135894
 2:02 pm on May 20, 2010 (gmt 0)

Well the code works if I don't use the full file name:

google1aad878a5v471m4.html instead off
google1aad878a5v471m44.html

To explain things further:

the new domain is on the same ftp but in a subfolder
so google1aad878a5v471m44.html is also in the subfolder
so you have
/google1aad878a5v471m44.html (which does not need a redirect)
and
/example-folder/google1aad878a5v471m44.html

All the other pages get redirect to new domain aka the /example-folder/.

Could that be the issue?

glimbeek




msg:4135895
 2:05 pm on May 20, 2010 (gmt 0)

I test in 5 different browsers across 2 pc's when I get this confused.

I tested your code but that also redirects to the new domain where the file is also located.

google1aad878a5v471m44.html was the file

google1aad878a5v471m.html gets redirecten as well now?

glimbeek




msg:4135910
 2:27 pm on May 20, 2010 (gmt 0)

Did some more testing and it really looks like the cause for the file ALWAYS redirecting is the fact that the same file can be found in the subfolder. The subfolder in which the files for the new domain are located. Is there a way to work around this?

jdMorgan




msg:4135915
 2:37 pm on May 20, 2010 (gmt 0)

Disable MultiViews and AcceptPathInfo if you are not using them. Example:

AcceptPathInfo Off
Options +FollowSymLinks -Indexes -MultiViews

AcceptPathInfo is available only on Apache 2.x servers. You'll get an error trying to enable or disable in on Apache 1.3x.

Jim

jdMorgan




msg:4135958
 3:42 pm on May 20, 2010 (gmt 0)

Just a note on the google verification files...
Just in case you may have to get a new verification code in the future, you can exclude all valid google verification file requests in the top-level directory using

RewriteCond $1 !^(google|noexist_)[0-9a-f]{16}\.html$

Note that the "noexist_" request is how google tests your 404 response, and you certainly don't want to mess that up with a redirect...

Jim

glimbeek




msg:4136370
 6:49 am on May 21, 2010 (gmt 0)

Thanks for the reply jdMorgan,

AcceptPathInfo Off
Options +FollowSymLinks -Indexes -MultiViews

These are all kind off new to me.

At the moment I start my .htaccess file with:
Options +FollowSymlinks
RewriteEngine on

What does AcceptPathInfo Off do?
And what does -Indexes -MultiViews do?
Do I need to make sure I don't do anything else that might interfere with these new additions?

glimbeek




msg:4136371
 6:50 am on May 21, 2010 (gmt 0)

It's weird though...

Did a quick test

and if I upload thefile.html to both location and add a rule for thefile.html so it doesn't get redirected it works? As in it doesn't get redirected.

So why does it work for thefile.html

but not for my google verification file?

Ps.

I'm working with the following:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^www.exam-ple.com [OR]
RewriteCond %{HTTP_HOST} ^exam-ple.com
RewriteCond %{REQUEST_URI} !^/thefile\.html$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Even though the code could be "simplified and improved", I know what this does so I want to get it to work with this and then switch to your suggestion after I figured out what causes the weird behaviour.

jdMorgan




msg:4136597
 3:09 pm on May 21, 2010 (gmt 0)

Please take the initiative -- I have very limited time to volunteer here repeating well-documented information, or to spend trying to convince you to test my recommendations. (Thanks)

What does AcceptPathInfo Off do? [httpd.apache.org]
And what does -Indexes -MultiViews do? [httpd.apache.org]

Jim

glimbeek




msg:4138223
 6:09 am on May 25, 2010 (gmt 0)

Thanks for the reply jdMorgan!

I understand you have very limited time and I appreciate your support. I read both pages before asking, but I didn't fully understand.

I will try your recommendations and see how I do.

George

glimbeek




msg:4138247
 7:19 am on May 25, 2010 (gmt 0)

That's odd, I replied but nothing showed up?

Anyway, I tried your solution and it works.

Thanks for the great support Jim. I'm still not a 100% how your solution works though.

George

jdMorgan




msg:4138467
 3:07 pm on May 25, 2010 (gmt 0)

Read the documentation on MultiViews (mod_negotiation) and AcceptPathInfo (core). They do somewhat-different things but in both cases, the key idea is that each function acts if the requested URL-path does not resolve to an existing file.

Either of them can pre-empt mod_rewrite, causing a rewriterule to appear to fail. In fact, the problem is not that the rule is failing, but rather that the rewriterule is never applied if MultiViews or AcceptPathInfo runs first.

When you said that your code worked if the file existed, that indicated to me that the likely problem was the action of MultiViews and/or AcceptPathInfo.

Glad you got it working!

Jim

glimbeek




msg:4139742
 6:48 am on May 26, 2010 (gmt 0)

Thanks again for the support and explaining it to me.

Regards,

George

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