Forum Moderators: phranque

Message Too Old, No Replies

Rewrite Rule help

         

jalperin

10:03 pm on Mar 17, 2005 (gmt 0)

10+ Year Member



I'm trying to take all references to [domain.com...] . . and change them to [domain.com...] . .

Why doesn't something like this work?
RewriteRule ^clients/([AZaz09]+)$ ccc/clients/$1

I've put this in an .htaccess file in the root of my htdocs. Using Apache 2 if it matters.

Thanks for any help.

jdMorgan

12:59 am on Mar 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jalperin,

Welcome to WebmasterWorld!

Which URL actually exists?

Do you have other existing RewriteRules that work?

Do you get any error messages? If so, please post the relevant entries from your server error log.

Jim

jalperin

3:01 am on Mar 18, 2005 (gmt 0)

10+ Year Member



I am entering: [localhost...] The actual url is [localhost...]
(with the /ccc/).

When I try testing, I get a 404 - not found as follows:
The requested URL /clients/example.php was not found on this server.

In my server error log, I find this error message:
[Thu Mar 17 21:55:09 2005] [error] [client 127.0.0.1] File does not exist: C:/Documents and Settings/Jeff/My Documents/My Webs/clients

[Note: My DocumentRoot is set to: C:/Documents and Settings/Jeff/My Documents/My Webs

The .htaccess file in which the RewriteRule lives is located in the same DocumentRoot location. ]

jdMorgan

2:40 pm on Mar 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this:
[code]
# Following line may not be needed if already present in httpd.conf (and may cause an error if so)
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^clients/(.+)$ /ccc/clients/$1 [L]
[code]
Jim

jalperin

2:57 pm on Mar 18, 2005 (gmt 0)

10+ Year Member



Cool! That worked, but . . .

. . . graphics, stylesheets and other stuff on the page can't be found so the page displays very poorly. They are referred to in the html in the page using relative addressing (e.g. <link href="../styles.css" rel="stylesheet" type="text/css"> )

Is there a technique to rewrite these as well so the page works?

--ja

jdMorgan

3:04 pm on Mar 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you may need to add one or more RewriteCond lines to prevent requests for "common" elements from being rewritten. You'll need to analyze the situtation and develop a plan to exclude by filetype, path, or by both, whichever is more efficient for your situation, but an example would be:

# exclude image and css files from rewrite
RewriteCond %{REQUEST_URI} !\.(jpe?g¦gif¦png¦css)$
... rule follows

Change the broken pipe "¦" characters to solid pipe (usually Shift-\) characters before use!

[added] May I also suggest that you use the "relative-to-root" relative paths whenever possible? This makes it a lot easier to move things around and prevents many problems, while retaining the advantages of relative paths. (e.g. <link href="/common_stuff/styles.css" rel="stylesheet" type="text/css"> ) This path is always resolved relative to the Web root, so the browser always 'starts' looking in a known directory -- the top-level directory of your domain, as opposed to starting wherever it thinks the current location is. Doing this may resolve your problem without changing the mod_rewrite code. [/added]

Jim

Longhaired Genius

3:41 pm on Mar 18, 2005 (gmt 0)

10+ Year Member



Of course! I'm going to start doing that today!

jalperin

3:57 pm on Mar 18, 2005 (gmt 0)

10+ Year Member



Actually, maybe rewrite is the wrong tool for what I'm really trying to do. Here's my situation:

I have a production server at domain.com with a hard-coded link in my nav bar to [domain.com...] The problem with that is that when I try to test the code on my local development box, clicking the login link takes me to the production server. So I made the link dynamic using php $_SERVER['HTTP_HOST'] -- that is, [<?php...] echo $_SERVER['HTTP_HOST'];?>/login.php.

The problem with that is that the documentroot of my development server contains the roots of several different sites. That is:
/htdocs
/htdocs/ccc
/htdocs/website1
/htdocs/website2

So, the ssl login page on my development machine is [localhost...] instead of the [domain.com...] on the production server.

I could change my php coding so that if $_SERVER['HTTP_HOST'] is 'localhost', it adds in the /ccc/, but then I have problems if I ever change the directory name or location on my development box. And I guess I'd still have the problems with other links on the page not being written root relative.

Should I be doing something other than rewrite? Setting up a vhost maybe? Any other thoughts?

Thanks for all your help!

--Jeff