Forum Moderators: phranque

Message Too Old, No Replies

.htaccess

         

sunshineteam

9:42 pm on Feb 3, 2010 (gmt 0)

10+ Year Member



I have Apache on a Linux server and I have many domain names on the same space.

I need these 3 names to point to just the 1 name:

example.com
example-two.com
got-example.net
(and www versions)

to

www.got-example.net/

And redirect the existing:
www.example.com/why-use-a-pet-sitter to http://www.example.com/got-example/why-pet-sitting.php
www.example.com/services-and-rates to http://www.example.com/got-example/services.php
www.example.com/rates to http://www.example.com/got-example/rates.php
www.example.com/about-us to http://www.example.com/got-example/about-us.php
www.example.com/contact-us to http://www.example.com/got-example/contact-us.php
www.example.com/my-critters to http://www.example.com/got-example/my-critters.php
www.example.com/references to http://www.example.com/got-example/testimonials.php
www.example.com/favorite-links to http://www.example.com/got-example/links.php

(and renaming .php pages to .htm)

I'm new to .htaccess but have been reading up on it the last week or so. (It's a slow, painful process with some conflicting information I've found.)

This is (part of) my .htaccess file so far:

RewriteEngine on
Options +FollowSymlinks
#This stops everybody from using my images except list below
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.com(/)?.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example-two.com(/)?.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?got-example.net(/)?.*$ [NC]
RewriteRule \.(gif|jpg|js|css|png)$ - [F,NC,L]
#
#This blocks bad bots and site rippers
RewriteCond %{HTTP_USER_AGENT} ^BlackWidow [OR]
RewriteCond %{HTTP_USER_AGENT} ^Bot\ mailto:craftbot@yahoo.com [OR]
RewriteCond %{HTTP_USER_AGENT} ^ChinaClaw [OR]
#etc etc...
RewriteCond %{HTTP_USER_AGENT} ^Zeus
RewriteRule ^.* - [F,L]
#
#Prevent people from reading this htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
#
# Protect files and directories from prying eyes.
<FilesMatch "\.(engine|inc|info|install|module|profile|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(code-style\.pl|Entries.*|Repository|Root|Tag|Template)$">
Order allow,deny
</FilesMatch>
#
# Don't show directory listings for URLs which map to a directory.
Options -Indexes
#
Redirect 301 /why-use-a-pet-sitter http://www.example.com/got-example/why-pet-sitting.php
Redirect 301 /services-and-rates http://www.example.com/got-example/services.php
Redirect 301 /rates http://www.example.com/got-example/rates.php
Redirect 301 /about-us http://www.example.com/got-example/about-us.php
Redirect 301 /contact-us http://www.example.com/got-example/contact-us.php
Redirect 301 /my-critters http://www.example.com/got-example/my-critters.php
Redirect 301 /references http://www.example.com/got-example/testimonials.php
Redirect 301 /favorite-links http://www.example.com/got-example/links.php
#
#Below makes everything www. version
RewriteCond %{HTTP_HOST} (.*)got-example\.net [NC,OR]
RewriteCond %{HTTP_HOST} (.*)example-two\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^/?example\.com [NC]
RewriteRule .? http://www.example.com/got-example/index.php [L,R=301]


One problem I have is that I have a few other sites on this server. When anybody types www.example.com they get a 404 page. If they just do example.com (without www) or www.got-example.net or got-example.net it works fine.

Another problem I have here is that I don't know how to get the current .php files to show up as .htm files. (I just haven't gotten to that part yet after several days of ironing out this www.example.com - 404 kink.)

How's my syntax on the .htaccess file?

[edited by: jdMorgan at 10:51 pm (utc) on Feb. 3, 2010]
[edit reason] examplified. Please see TOS and Charter. [/edit]

sunshineteam

10:40 pm on Feb 3, 2010 (gmt 0)

10+ Year Member



I got the page to load by changing my code to this:


#Below makes everything www. version
RewriteCond %{HTTP_HOST} (.*)got-example\.net [NC,OR]
RewriteCond %{HTTP_HOST} (.*)example-two\.com [NC]
RewriteRule .? http://www.example.com/got-example/index.php [L,R=301]
#
RewriteCond %{REQUEST_URI} !^/got-example/index.php$ [NC]
RewriteCond %{HTTP_HOST} ^www\.example.com(/)?$ [NC]
RewriteRule .? http://www.example.com/got-example/index.php [L,R=301]


However, now it appears images don't load. (Or css or scripts.)

I don't know if it's a slow server or what.

I've exhausted my mind.

[edited by: jdMorgan at 10:54 pm (utc) on Feb. 3, 2010]
[edit reason] Examplified. Please see TOS and Charter. [/edit]

jdMorgan

11:49 pm on Feb 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your rule is doing what you told it to do -- redirecting pretty much everything to index.php -- including image and css requests.

I would suggest that you slow down (a lot) and read some of the related threads here in the forum and in our Apache Forum Library, and then start over. The code above has many, many errors -- errors in logic, errors in coding, errors in rule order, errors in regular expressions patterns and anchoring, inefficient regex patterns, mod_alias redirects mixed with mod_rewrite rewriterules, external redirects when internal rewrites might be more appropriate, etc. - even inaccurate comments. Not to mention that it was entirely unnecessary to change your *URLs* just because you switched your *files* from .html to .php.

The one thing that may not be obvious about mod_rewrite is that it requires precision -- brain surgery precision, space shuttle design precision, jeweler's precision. It is unforgiving of the tiniest mistakes, and these mistakes can badly affect your search engine ranking, even if they don't knock your server off-line.

I'd like to help, but frankly, there's just too much, and I don't even know where to start...

Maybe with your last code snippet, but you'll need to look at the changes and apply many of them to your other rules as well:

# Redirect all non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^.]+\.)*(example\.com|example-two\.com|got-example\.net)
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# Internally rewrite all 'page' requests to index.php script
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|ico|css|js|txt|pdf|xml)$
RewriteCond %{REQUEST_URI} !^/got-example/index.php$
RewriteRule ^ /got-example/index.php [L]

Jim