too many rewrites?

Discuss practical ways rearrange URLs using mod_rewrite.

too many rewrites?

Postby canadave » Sat Oct 17, 2009 6:45 pm

Hi,

I'm an htaccess newbie....trying to set up my webspace with multiple domains properly. There are several considerations that I'm trying to take into account at once, and as a result, things aren't set up properly. Just wondering if someone can help?

Here's the picture so far:

I have a webspace with one "default" domain that was set up when I purchased the webspace, called "infinitives.ca"; plus there's a few other "addon" domains I set up afterward. I'll number those addon domains "domain1", "domain2", etc. (their actual names aren't important to this story, I don't think.)

To clean up my /public_html folder, I decided to set things up so that each domain--the default one and the addon ones--is in its own separate folder inside public_html, like this:

/public_html
|
|----> infinitives.ca
|----> domain1.ca
|----> domain2.ca

That way, I can neatly work with each website in its own dedicated folder, can run external installation scripts for web applications (which require public_html permissions to function properly), etc. Neat, eh?

Since requests to infinitives.ca, by default, would go to /public_html first, and then not find an index file, I set up an htaccess file inside /public_html to direct those requests to the infinitives.ca subfolder. That works perfectly. I also have it set up to remove any "www." at the beginning of URLs....I want that to be in effect for ANY URL visitors go to on my sites. I hate the "www."

Next, I have a subfolder inside infinitives.ca called "RKE", which is basically a separate website in its own right from the rest of infinitives.ca (it's a site for the band I'm in...nothing to do with infinitives.ca, which is my business site, but I needed to put RKE's website somewhere, and I had the webspace, so...*shrug*). I want requests to "infinitives.ca/rke" to get rewritten as "infinitives.ca/RKE", because RKE is an acronym, and it looks silly in lowercase. In addition, since the default index file in RKE is "default.php", I need a DirectoryIndex call that lets the server know that.

The last paragraph is the part I'm having the problem with. I have no idea where the htaccess to handle those two things goes, or how to phrase it properly, or how to remove the "www." there as well, or how to prevent everything from conflicting with the htaccess file in /public_html. I've tried putting an htaccess file in /public_html/infinitives.ca, and also an htaccess file in /public_html/infinitives.ca/RKE, but no luck. In fact, I seem to have screwed things up a bit.

Right now, if you go to infinitives.ca/RKE/ (note the last slash), it's fine. But if you go to infinitives.ca/RKE (without the last slash), the URL gets written as "http://infinitives.ca/infinitives.ca/RKE/" . That's probably because the FIRST htaccess file, the one that was supposed to sort out the domains in /public_html, is not configured right. And of course, as you can see, infinitives.ca/rke doesn't get rewritten to infinitives.ca/RKE; in fact, it returns a 404 error, because there is no such directory as "rke" (case sensitive).

*deep breath*.....so.....HELP! lol :) Can someone help make sense of all this?

If it helps, here's what the htaccess file in /public_html has right now:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} infinitives.ca
RewriteCond %{REQUEST_URI} !infinitives.ca/
RewriteRule ^(.*)$ infinitives.ca/$1 [L]

There's also an htaccess file in the /public_html/infinitives.ca folder, which has this:
# mod_rewrite in use
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^infinitives\.ca
RewriteRule (.*) http://infinitives.ca/$1 [R=301, L]


And, last but not least, in /public_html/infinitives.ca/RKE folder, there's this htaccess file:
DirectoryIndex default.php

What a mess :(
canadave
 
Posts: 8
Joined: Sat Dec 06, 2008 10:24 am

Postby richardk » Mon Oct 19, 2009 11:08 am

Try the following in ./public_html/.htaccess
Code: Select all
# Check for the following index files (in this order).
DirectoryIndex index.html index.php index.htm default.php
Options +FollowSymLinks

RewriteEngine On

# Upper-case infinitives.ca/RKE/. (And remove www.)
RewriteCond %{HTTP_HOST} ^(www\.)?(infinitives\.ca)$ [NC]
RewriteCond $2 !^RKE$
RewriteRule ^(infinitives\.ca/)?(rke)(/(.*))?$ http://%2/RKE/$4 [R=301,L]

# Add missing trailing slashes. (And remove www.)
RewriteCond %{HTTP_HOST} ^(www\.)?(infinitives\.ca)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/infinitives.ca%{REQUEST_URI}/ -d
RewriteRule [^/]$ http://%2%{REQUEST_URI}/ [R=301,L]

# Remove all www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]

# Rewrite infinitives.ca to /infinitives.ca
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^infinitives\.ca$ [NC]
RewriteRule .* /infinitives.ca%{REQUEST_URI} [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby canadave » Tue Oct 20, 2009 8:03 am

Thanks very much for the reply, Richard :) I assume you wanted me to remove the .htaccess files from the other folders, and just leave the only .htaccess file as being in /public_html?

Here's the results once I did that and changed the .htaccess file in public_html as you suggested:

infinitives.ca: Returned a "500 Internal Server" error.
www.infinitives.ca: Redirected to infinitives.ca (good!), which then returned the 500 error (bad).
infinitives.ca/RKE: Returned a "500 Internal Server" error.
infinitives.ca/rke: Redirected to /RKE (good!), which then returned the 500 error (bad).

I've changed it back for now.

Does it make a difference that my webhost is running LiteSpeed rather than Apache? I was under the impression that there was no difference in terms of mod_rewrites, but maybe I'm wrong, so I figured I should mention it now.
canadave
 
Posts: 8
Joined: Sat Dec 06, 2008 10:24 am

Postby richardk » Tue Oct 20, 2009 9:11 am

Does it make a difference that my webhost is running LiteSpeed rather than Apache?

It might use the same syntax (i don't know) but it might not have the same variables available. Try replacing
Code: Select all
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^infinitives\.ca$ [NC]
RewriteRule .* /infinitives.ca%{REQUEST_URI} [QSA,L]

with
Code: Select all
RewriteCond %{HTTP_HOST} ^infinitives\.ca$ [NC]
RewriteRule ^infinitives\.ca(/.*)?$ /infinitives.ca%{REQUEST_URI} [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby canadave » Tue Oct 20, 2009 9:48 am

Hmmm...well, that's an improvement, anyway! :)

Now here's the results after making the change you suggested:

infinitives.ca: Now shows a raw directory list of infinitives.ca, domain1.ca, etc. (i.e. it says "Index of /")
www.infinitives.ca: Redirected to infinitives.ca (good!), which then returned the folder list as described above (bad).
infinitives.ca/RKE: Redirects to infinitives.ca/RKE/ (note the trailing slash), and then returns a "404 Not Found" error.
infinitives.ca/rke: Redirected to /RKE/, which then returns the same 404 Not Found error as described above.

So....close.......lol
canadave
 
Posts: 8
Joined: Sat Dec 06, 2008 10:24 am

Postby richardk » Tue Oct 20, 2009 10:44 am

Doh. There should have been a ! before
Code: Select all
^infinitives\.ca(/.*)?$


There is also a missing NC flag.

Code: Select all
# Check for the following index files (in this order).
DirectoryIndex index.html index.php index.htm default.php
Options +FollowSymLinks

RewriteEngine On

# Upper-case infinitives.ca/RKE/. (And remove www.)
RewriteCond %{HTTP_HOST} ^(www\.)?(infinitives\.ca)$ [NC]
RewriteCond $2 !^RKE$
RewriteRule ^(infinitives\.ca/)?(rke)(/(.*))?$ http://%2/RKE/$4 [NC,R=301,L]

# Add missing trailing slashes. (And remove www.)
RewriteCond %{HTTP_HOST} ^(www\.)?(infinitives\.ca)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/infinitives.ca%{REQUEST_URI}/ -d
RewriteRule [^/]$ http://%2%{REQUEST_URI}/ [R=301,L]

# Remove all www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]

# Rewrite infinitives.ca to /infinitives.ca
RewriteCond %{HTTP_HOST} ^infinitives\.ca$ [NC]
RewriteRule !^infinitives\.ca(/.*)?$ /infinitives.ca%{REQUEST_URI} [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby canadave » Tue Oct 20, 2009 11:00 am

OK, well, I may not know a whole lot about mod_rewrites, but I know brilliance when I see it! :) Thank you so much...everything works perfectly now!!!! Spot on!

Incidentally, is there a good resource that explains in detail the complete syntax used in mod_rewrite? Something designed to help laypeople learn how to write detailed mod_rewrites? I've had a look 'round Google but can't seem to find anything good....
canadave
 
Posts: 8
Joined: Sat Dec 06, 2008 10:24 am

Postby richardk » Wed Oct 21, 2009 10:01 am

The Apache Docs have a lot more information than they used to: http://httpd.apache.org/docs/2.2/rewrite/.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 21 guests

cron