Forum Moderators: phranque
I am a big fan of your boards here and have used them many times in the past to troubleshoot problems. However, I have been working on a mod rewrite issue now for two days and my productivity is rapidly decreasing.
Heres the idea:
I want http://www.example.com/name/anything/ to rewrite (not redirect) to http://www.example.com/anything?client=name. I realize this is not optimum to have my first folder be the query instead of the last, but I need it for presentation purposes. I have also explored subdomains: [name.example.com...] but I am hosted on GoDaddy and the wildcard DNS creates other problems.
After playing around for two days, I have come up with the following:
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^([^/]*){1}/(.*)$ /$2?client=$1&$2 [L]
I put in the Rewrite Condition because it is also possible NOT to have the client name. So if anyone notices some minute error that I have overlooked, I would really appreciate some help here.
Thanks,
Matt
I'd suggest:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ /$2?client=$1 [L]
Jim
I had the additional $2 on the string to assist in the debugging.
So with the configuration that you suggested, a couple of things are happening:
1) The style sheet is not being included, which is probably just a side effect.
2) $1 always seems to reference the last folder in the URL
Here are the test values I attempted (the only folders on the server are includes, images, and login):
- http://www.example.com/test/
$1 = "test"
$2 = ""
- http://www.example.com/test/login/
(This is the important test, it should reference http://www.example.com/login/?client=test)
$1 = "login"
$2 = "index.php"
- http://www.example.com/test/login/check/
$1 = "check"
$2 = ""
So you can see, it always seems to match the last folder instead of the first.
I want http://www.example.com/name/anything/ to rewrite...
You've got to give mod_rewrite *something* to key off of, something to differentiate a URL-path that should be rewritten from one that shouldn't. If "anything" in the sentence above means "anything or nothing" then that "anything" may include additional path information.
You may also need to take steps to prevent recursion, for example by adding a check to be sure that the rewritten URL isn't rewritten again; I'll guess this might be accomplished with an additional 'file exists' check using -f, but it all depends on precisely what URL-paths you do and don't want to rewrite.
So here's a different twist, which may be too specific; It requires that only two URL-path-parts be present in the requested URL, and adds the requirements that the requested URL-path ends with a slash and does not exist as a file.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/$ /$2?client=$1 [L]
If this were my site, I'd redesign the URL architecture, adding a path-part that explicitly identifies the URLs to be rewritten to the client= format. For example, adding "clients" to the URLs:
example.com/clients/<client_name>/<directory_as_in_your_original_example>/
The problem you're seeing with CSS (and probably images and other included elements) may be that you're using page-relative URLs for these items; Remember that it is the browser that resolves relative URLs, and tht if you use page-realtive links, then the browser will resolve your CSS include as something like /client/directory/styles.css, while the actual server path is /directory/styles.css. The solution is to either use server-relative URLs or absolute URLs, or to add additional rewriterules and/or conditions to rewrite or not rewrite the requests for these included objects as desired. I suggest getting the basic rule and URL-architecture working before bothering with this secondary issue.
Jim
As I said in my original post, I realize that this is not optimum for performance, and if I could get the subdomains to work, I would prefer that. As it stands now, I added the additional Condition: RewriteCond %{REQUEST_FILENAME}!-f and the site is performing as I had hoped (as long as the file exists).