Forum Moderators: phranque

Message Too Old, No Replies

Complex Rewriting and Redirecting issues

         

Mikroz

6:21 am on May 22, 2012 (gmt 0)

10+ Year Member



Hi all,

I do apologise for the lack 'example.com' here, but I am working with multiple domains and wasn't sure how to go about it.

I have one of the most complex .htaccess files I've personally encountered, parly because I need it and partly because it's forcing me to learn, which I am enjoying. Considering I'm only an amateur tinkerer with this stuff, pushing the envelope not always a good recipe. :D

It's giving me hassles at the moment and I could surely do with some help, please.

## Domain exclusions ("(www.)example.co.za" and blank)
# Parked bbb.za.org Redirect exclusion
RewriteCond %{HTTP_HOST} ^(www\.)?bbb\.za\.org
RewriteRule (.*) http://blog.aaa.com/sub1/sub2/$1 [R=301,L]

# Other Addon/Parked exclusions
RewriteCond %{HTTP_HOST} !^((www\.)?ccc\.co\.za\.?(:[0-9]+)?)?$
RewriteCond %{HTTP_HOST} !^((www\.)?ddd\.za\.net\.?(:[0-9]+)?)?$
RewriteCond %{HTTP_HOST} !^((www\.)?eee\.za\.org\.?(:[0-9]+)?)?$

# Directory exclusions
RewriteCond %{REQUEST_URI} !^/(_attachments)/

# File exclusions
RewriteCond $1 !^(page\.php|sitemap\.xml|sitemap\.xml\.gz|robots\.txt|google##########\.html)$

# All other requests to...
RewriteRule ^(.*)$ http://blog.aaa.com/$1 [R=301,L]

# Sitemap redirection
Redirect 301 /sitemap.xml [url]http://blog.aaa.com/sitemap.xml[/url]

# Redirect if subfolder index is requested (Signature/s, etc.)
RewriteRule ^_attachments/?$ [url]http://blog.aaa.com/[/url] [R=301,L]

# Redirect if non-existent file in subfolder is requested
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^_attachments/(.+)$ http://blog.aaa.com/$1 [R=301,L]


I'm hoping you're all going to tell me that that horror is actually not that bad, but I suspect I'm about to be crucified by you lot. :D

As you might imagine, it's not working correctly...

1. When you request aaa.com/sitemap.xml it is not pulling it from blog.aaa.com/sitemap.xml and I am not sure whether ^ this is the problem or if it has something to do with needing to free up that file in the blog folder's .htacess file.

2. Secondly, I have another subfolder of aaa.com that is getting forwarded to either blog.aaa.com or showing up a 301 error no matter what solutions I try. It is supposed to be a standalone subdomain.

Am I perhaps missing something in the .htaccess files in the specific folders or is the problem only in here, from what you can see?

Can you help, please?

Thank you.

lucy24

10:08 pm on May 22, 2012 (gmt 0)

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



Hate to break it to you, but you do not have the world's most complicated htaccess. But at least your question has only been asked about 1000 times before, instead of the normal 10000+ (Unfortunately this is really true.)

I do apologise for the lack 'example.com' here, but I am working with multiple domains and wasn't sure how to go about it.

You can say example dot anything. example.com, example.org, example.co.za -- even tld's that don't really exist. (Except ###, which will run afoul of Word Censors.) Hm, you seem to have goofed in the vicinity of "google" ;)

Can you backtrack please and start by saying in English what you're trying to do? It always works out better if you first figure out what you want to do and then write the code-- instead of first writing the code and then having to say Oops, it's not doing what I wanted.

Always put the most specific rules first. If you want a Rewrite to apply to all requests except foobar.html, try to arrange the rules as

RewriteRule foobar\.html blahblah

RewriteRule other stuff here


instead of

RewriteCond %{REQUEST_URI} !foobar\.html
RewriteRule blahblah

RewriteRule foobar\.html blahblah

g1smd

10:48 pm on May 22, 2012 (gmt 0)

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



Rule order is an issue. Rule with (.*) pattern must be last.

Change the rule using "Redirect" to instead use a RewriteRule. Don't mix code from different Apache modules in the one htaccess file.

sitemap\.xml|sitemap\.xml\.gz
simplifies to
sitemap\.xml(\.gz)?
as do several other rules.

Mikroz

12:08 pm on May 23, 2012 (gmt 0)

10+ Year Member



I'd like to see that file, lucy. ;)

I am aware that my request is amongst the throngs found here, for which I apologise, but the seemingly infinite number of sites I've browsed so far haven't helped me much.

Thanks for your help thus far, both of you. It is greatly appreciated.

I've edited my code in accordance with your suggestions. I haven't managed to get my site running offline as yet, so I'm not keen to mess with the live one for testing.

# File exclusions
RewriteCond $1 !^(page\.php|sitemap\.xml(\.gz)?|robots\.txt|google1234567890\.html)$

# Directory exclusions
RewriteCond %{REQUEST_URI} !^/(_attachments)/

## Domain exclusions ("(www.)example.co.za" and blank)
RewriteCond %{HTTP_HOST} !^((www\.)?ccc\.co\.za\.?(:[0-9]+)?)?$
RewriteCond %{HTTP_HOST} !^((www\.)?ddd\.za(\.net|\.org)?(:[0-9]+)?)?$
# Parked bbb.za.org exclusion
RewriteCond %{HTTP_HOST} ^(www\.)?bbb\.za\.org
RewriteRule (.*) http://blog.aaa.com/sub1/sub2/$1 [R=301,L]

# Sitemap redirection
RewriteRule ^sitemap.xml(\.gz)?$ blog/sitemap.xml(\.gz)?

# All other requests to...
RewriteRule ^(.*)$ http://blog.aaa.com/$1 [R=301,L]

# Redirect if subfolder index is requested
RewriteRule ^_attachments/?$ [url]http://blog.aaa.com/[/url] [R=301,L]

# Redirect if non-existent file in subfolder is requested
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^_attachments/(.+)$ http://blog.aaa.com/$1 [R=301,L]


Certain files need to be accessible via the root. E.g. phpinfo.php

Requests are made to the server for specific files, which are hot-linked. _attachments.

Various secondary domains point here, all of which must NOT be redirected. One of these, bbb, is directed somewhere else.

Requests for sitemap.xml(.gz) are directed to a file in another folder.

Everything else goes to blog.aaa.com

If any requests to any non-existant files or the _attachments directory itself are made, they are redirected.

g1smd,

What do you mean by mixing code from different modules? How does one operate multiple .htaccess files? I've been enclosing the different sections in the relevant tags, such as <IfModule mod_rewrite.c>

Many thanks.

wilderness

1:56 pm on May 23, 2012 (gmt 0)

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



What do you mean by mixing code from different modules?


Two different modules:

# All other requests to...
RewriteRule ^(.*)$ http://blog.aaa.com/$1 [R=301,L]

# Sitemap redirection
Redirect 301 /sitemap.xml [url]http://blog.aaa.com/sitemap.xml[/url]

wilderness

1:59 pm on May 23, 2012 (gmt 0)

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



How does one operate multiple .htaccess files?


By creating additional htaccess within specific directories and/or sub-directories, as opposed to one file in the root directory.

Mikroz

4:12 pm on May 23, 2012 (gmt 0)

10+ Year Member



Thanks, wilderness.

I do have other .htaccess files in use, for specific instructions.

This particular one is controlling the root and provides global settings.

g1smd

6:40 pm on May 23, 2012 (gmt 0)

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



What do you mean by mixing code from different modules?

Don't mix code from different Apache modules in the one htaccess file. Change the rule using "Redirect" (mod_alias) to instead use a RewriteRule (mod_rewrite).

The "# Sitemap redirection" code can never work. The syntax is wrong (and with basic correction would work as a rewrite, not a redirect).