Forum Moderators: phranque

Message Too Old, No Replies

re-write rules and I are not getting along

rewrite rules

         

nkad05

9:20 pm on Jul 20, 2005 (gmt 0)

10+ Year Member



Here is the situation, I am trying to test this on my win32 box with Apache. Basically, I am trying to write a rule that will take everything after the host name (i.e. [somedomain.com...] and put it in an environment variable so I can access it via php. For example: [somedomain.com...] (floridanews should go in the environment variable, and the webserver should call index.php to read out the variable.)

Ok so here is my attempt (doesnt work) :(

RewriteEngine on
RewriteLog "c:\urlrewrite.log"
RewriteLogLevel 9
RewriteRule ^(/+) [E=URL:$1]
RewriteRule /index.php [R]

Ok, laugh all you want. I know, I deserve it.

ChadSEO

10:04 pm on Jul 20, 2005 (gmt 0)

10+ Year Member



You're on the right track. I haven't done this specifically before, but hopefully this will get you a little closer.

RewriteEngine on
RewriteLog "c:\urlrewrite.log"
RewriteLogLevel 9
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^/(.+)$ /index.php [E=URL:$1]

I'm assuming this is from your httpd.conf file... if it's from an .htaccess file, then remove the slash (/) in "^/(.+)$"

[edited by: jdMorgan at 10:15 pm (utc) on July 20, 2005]
[edit reason] Fixed dropped space before "!" w/bold tags [/edit]

nkad05

12:02 am on Jul 21, 2005 (gmt 0)

10+ Year Member



Is there an easy way to debug these things? lol.

nkad05

5:40 pm on Jul 21, 2005 (gmt 0)

10+ Year Member



ChadSEO, I tried the changes that you made, but I am getting a 404 Not Found Error. It seems like it's not hitting the index.php file.

[domain.com...]

I'm trying to get url part 'test' into an ENV variable and have the request for [domain.com...] redirected to index.php or some other file, so i can process the ENV variable.

jdMorgan

7:27 pm on Jul 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm assuming this is from your httpd.conf file... if it's from an .htaccess file, then remove the slash (/) in "^/(.+)$"

Where are you putting your code?

Do you get an entry in your error log?

Have you tested with a simple rule, like


RewriteRule ^/silly\.html$ /index.php [L]

requesting "/silly.html" to see if you get the contents of "/index.php"?

Jim

nkad05

10:38 pm on Jul 21, 2005 (gmt 0)

10+ Year Member



Yes, my code is in an .htaccess file

I got it working almost the way I want:


RewriteEngine on

RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d

RewriteCond %{HTTP_HOST} ^www.mydomain\.net$
RewriteCond %{REQUEST_URI}!^/index.php$
RewriteRule ^(.+)$ http://www.mydomain.net/g.php?uid=$1 [R]

RewriteCond %{HTTP_HOST} ^mydomain\.net$
RewriteCond %{REQUEST_URI}!^/index.php$
RewriteRule ^(.+)$ http://mydomain.net/g.php?uid=$1 [R]

It works, except I can't figure out how to properly detect if the request is coming from www.mydomain.net or just mydomain.net. The second set of RewriteCond works, but the third set of RewriteCond doesn't. I can't figure out why.

jdMorgan

1:58 am on Jul 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> third set

There are only two sets of RewriteConds in the code you posted. RewriteConds are ANDed (unless you use the [OR] flag, and apply to the first RewriteRule that follows them, regardless of intervening blank or comment lines.

Therefore your checks for file exists and directory exists at the top apply only to the first RewriteRule. You will have to duplicate those RewriteConds if you wish them to apply to the second rule as well.

Also note that checks for file/directory exists are slow, since mod_rewrite must call your filesystem manager to do the check. If you use them, try to make those checks the last RewriteCond before the RewriteRule. When this is possible, it will improve the performance of your site.

Further, I recommend that you do not end-anchor domain names in the RewriteCond patterns. Any user-agent that appends a port number (e.g. example.com:80) will break your rule if it is end anchored.

Jim

jdMorgan

4:06 pm on Jul 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Another thought: Why are you doing the file exists check? If the answer is, "I don't know, but it won't work without it," then I should point out that it should not be necessary, except that the URI in your second RewriteCond does not match the URL-path given in the RewriteRule, which it should -- This prevents an infinite Rewrite loop if it is done properly.

You may need to add RewriteCond(s) to prevent images, CSS, JS, /w3c/p3p.xml, and robots.txt from being rewritten, but this is still far more efficient than doing file-exists checks.

By making the "www." in the first RewriteCond optional as shown, the second ruleset can be eliminated.

Also, using the [R] flag and a canonical URL in the substitution will result in an external redirect, which will "expose" your rewrite and cause your php pages with parameters to be listed in search results. This is usually very undersirable. I have therefore changed the RewriteRule to do an internal rewrite only.

Taking into account my note about anchoring domain names in the previous post above, this gives:


RewriteCond %{HTTP_HOST} ^[b](www\.)?[/b]mydomain\.net
RewriteCond %{REQUEST_URI} !^[b]/g\.p[/b]hp$
RewriteCond %{REQUEST_URI} !\.(gif¦jpg¦¦png¦js¦css)$ [NC]
RewriteCond %{REQUEST_URI} !^/(w3c/p3p\.xml¦robots\.txt)$
RewriteRule ^(.+)$ [b]/g.php?uid=$1[/b] [L]

Be sure to change the broken pipe "¦" characters to solid pipes from your keyboard before use; posting on this board modifies them.

Jim

nkad05

5:15 pm on Jul 22, 2005 (gmt 0)

10+ Year Member



Your right, I didn't need to check for index.php

Your code works, except for this part:


RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.net

So I tried to change it to:


RewriteCond %{HTTP_HOST} ^mydomain\.net [OR]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.net [OR]

But that didn't work either. Basically, the rule doesn't work if I go to [mydomain.net...] but it does work if I put in the www.

jdMorgan

6:29 pm on Jul 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Flush your browser cache and try again -- there's no reason that should not work.

The reason your version expanded to two RewriteConds did not work is that you have an [OR] flag on the second RewriteCond; Only the first RewriteCond should have the [OR] flag.

Jim

nkad05

6:49 pm on Jul 22, 2005 (gmt 0)

10+ Year Member



I should have realized I had an extra [OR].

It appears that my hosting provider has my domain set to redirect to www.mydomain.com if someone leaves off the www. Looks like i'll have to have a talk with them.

Thanks for all your help.