Forum Moderators: phranque

Message Too Old, No Replies

expert htaccess help needed please

         

CMYKreative

5:04 am on Dec 28, 2014 (gmt 0)

10+ Year Member



OK, we need a little help with some domain redirects from some experts please. I'll post the code we have currently in the .htaccess file a little further down, but I'll explain what we are trying to achieve:

1. primary domain (example.com in code below), should always use www prefix
2. primary domain should always use /en/ as the main "home" page
3. any parked domains on this account should redirect to primary domain

What we have below actually works and does everything we asked for above, however what we now want to do is redirect a few parked domains (not "all" parked domains) to specific urls, whilst leaving any others that are not specifically redirected continuing to redirect to the primary domain.

So, for example, if visitor accesses "parkedomain1.com" (with or without www), they would be redirected to "http://www.primary.com/category/page-01.php"

Or, if visitor accesses "parkedomain2.com" (with or without www), they would be redirected to "http://www.example.com/category/page-02.php"

But if visitor accesses "any" other parked domain, they'd continue to be redirected to the primary domain (a catchall as it were).

We're trying to make the .htaccess file as small as possible, and without having to specifically list "all" parked domains with specific redirects as it makes the file too large, we'd like to find a solution where we explicitly list the parked domains we want to redirect to specific urls only, and have a catchall for anything else not specifically listed.

Hope that makes sense, and hope you can help! :)

Here's the code we're using currently:

-------------------------------------

# Always use www in the domain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1example.com%{REQUEST_URI} [R=301]
# Always use www in the domain

# redirect ANY other domain that doesn't match www.example.com to main domain
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
# redirect ANY other domain that doesn't match www.example.com to main domain

# Set a default home directory, (this subfolder always loads)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ /en/ [R=301]
</IfModule>
# Set a default home directory, (this subfolder always loads)

# disable ETags #
Header unset ETag
FileETag None
# disable ETags #

# BEGIN add gZip compression
<IfModule deflate_module>
<IfModule filter_module>
AddOutputFilterByType DEFLATE text/plain text/html
AddOutputFilterByType DEFLATE text/xml application/xml application/xhtml+xml application/xml-dtd
AddOutputFilterByType DEFLATE application/rdf+xml application/rss+xml application/atom+xml image/svg+xml
AddOutputFilterByType DEFLATE text/css text/javascript application/javascript application/x-javascript
AddOutputFilterByType DEFLATE font/otf font/opentype application/font-otf application/x-font-otf
AddOutputFilterByType DEFLATE font/ttf font/truetype application/font-ttf application/x-font-ttf
</IfModule>
</IfModule>
# END add gZip compression

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
## EXPIRES CACHING ##

### BEGIN TIMEZONE
SetEnv TZ Asia/Bangkok
### END TIMEZONE

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

-------------------------------------

[edited by: phranque at 12:10 am (utc) on Dec 29, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

wilderness

9:26 am on Dec 29, 2014 (gmt 0)

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



Most all these questions are answered in this forum on a regular basis, which is obvious you haven't explored (else phranque would not have been required to edit your domain name).
GLOWING EXAMPLES that you failed to read previous threads and/or the forum charter and library:
1) repeated use of RewriteEngine on
2) repeated use of IfModule container

As to your multiple domain issue?
Simply to an archive search for domain2 or example2.

The purpose of this forum (see charter) is NOT to provide copy and paste solutions, rather allow you to learn and comprehend the process.

I apologize if I'm coming across rude, however if you wish courtesy than you must first convey courtesy.

lucy24

5:04 pm on Dec 29, 2014 (gmt 0)

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



what we now want to do is redirect a few parked domains (not "all" parked domains) to specific urls, whilst leaving any others that are not specifically redirected continuing to redirect to the primary domain.

By the time you read these replies you have probably figured out the answer on your own: within each category of rules, list them from most specific to most general. Problem solved.

I apologize if I'm coming across rude

The spirit was willing but the flesh was incurably polite, causing the fingers to type "glowing" when the brain wanted to say "glaring" ;)

CMYKreative

2:04 am on Dec 30, 2014 (gmt 0)

10+ Year Member



OK, thanks for the replies.

I've read MANY threads on MANY forums and have not found or understood the exact solution I was looking for with regards to the specific htaccess rules, hence asking the questions here.

If this is outside the scope of what this forum is intended for (which I assumed was for "experts" to assist "novices" with their questions and problems), then please feel free to remove the thread.

phranque

4:23 am on Jan 1, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, CMYKreative!


i would suggest internally rewriting to a script any requests recognizable as parked domains and then using that script to generate the appropriate redirect response.

But if visitor accesses "any" other parked domain, they'd continue to be redirected to the primary domain (a catchall as it were).

you should be cautious of any solution that will allow an infinite url space to be redirected to your home page.
if google tests this it will look like a "soft 404".

CMYKreative

6:03 am on Jan 1, 2015 (gmt 0)

10+ Year Member



Thank you phranque, we were trying to reduce the overall size of the htaccess file, without having to specifically list all domain redirects one after another.

As one of the other "members" posted earlier, you're not here to provide copy/paste solutions to specific questions, so we'll look elsewhere for the answers we're looking for then I guess.

Thanks for the help guys. Happy New Year.

phranque

6:26 am on Jan 1, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



please read the Charter for the Apache Web Server forum [webmasterworld.com].

i would be happy to give you more solutions to reducing the overall size of the htaccess file without having to specifically list "all" parked domains such as rewriting all the noncanonical hostname requests to a script.
but then someone would have to write the script.


or maybe you're looking for a solution like:
RewriteCond %{HTTP_HOST} !(parkeddomain1|parkeddomain2|example)\.com$ [NC]


but some of that wouldn't be necessary if the ruleset was placed after the explicitly listed the parked domains.

CMYKreative

7:16 am on Jan 1, 2015 (gmt 0)

10+ Year Member



Hi phranque, to be honest we've been looking for a solution for this for so long, and had so many conflicting suggestions/solutions (from other sources), we've found we get one part of it working correctly, but it then causes other issues in other areas.

p.s., your charter link just brought me right back here.

lucy24

8:25 am on Jan 1, 2015 (gmt 0)

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



or maybe you're looking for a solution like:
RewriteCond %{HTTP_HOST} !(parkeddomain1|parkeddomain2|example)\.com$ [NC]

My impression was that they're looking for something like

RewriteCond %{HTTP_HOST} parkeddomain1 [NC]
RewriteRule .? http://www.example.com/specialpage1 [R=301,L]

RewriteCond %{HTTP_HOST} parkeddomain2 [NC]
RewriteRule .? http://www.example.com/specialpage2 [R=301,L]

RewriteCond %{HTTP_HOST} !example\.com
RewriteRule .? http://www.example.com/genericpage [R=301,L]

Since parkeddomain1 and parkeddomain2 have been scooped up by the first two rules, the third rule only picks up the leftovers.

That's assuming example.com (your "real" domain) lives on the same server as your parked domains. If it doesn't, you don't need a Condition in the third rule, because requests for example.com will never see the rule.

CMYKreative

2:27 am on Jan 2, 2015 (gmt 0)

10+ Year Member



Hi Lucy, thank you.

Yes, essentially that's what we want to do, initially set some specific redirects for a few parked domains, and then collect anything else (that hasn't already explicitly been set) in a catchall to go to a different generic page.

One query, the [L] you've used on the end of each 'command' in your example, is this saying, complete the rule above, and then stop processing this rule and move onto the next rule, or should the [L] be used only once in htaccess on the 'final' rule?

lucy24

4:51 am on Jan 2, 2015 (gmt 0)

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



The [L] flag should be used with all RewriteRules except when
-- there is another flag, such as [F] or [G] (but not [R]), that automatically implies [L], or
-- you are absolutely certain you know what you are doing.

The [L] means "OK, we're done rewriting, move on to the next module" (and then possibly start over again from the top, depending on what the RewriteRule did).

It is very rare for a rule in [R] to not have [L] as well-- but it isn't impossible, which is why the flags are separate.

CMYKreative

3:06 am on Jan 3, 2015 (gmt 0)

10+ Year Member



OK, thank you lucy, we'll have a play around with the code again and see if we can get it to do what we want based on your suggestions.

Thanks.