Forum Moderators: phranque

Message Too Old, No Replies

Bad reference to index page?

         

premasagar

1:52 am on May 24, 2004 (gmt 0)

10+ Year Member



I have put this in my .htaccess file:

RewriteCond %{REQUEST_URI} ^/?$
RewriteCond %{QUERY_STRING} ^contact=(.*)
RewriteRule ^.*$ /misc/contact.php?contact=%1 [L]

This will basically allow a request to "http://www.mysite.com?contact=fred" to internally redirect to "http://www.mysite.com/misc/contact.php?contact=fred". It makes the address a bit easier on the eye.

My question is this: Technically, I think the address requested should be www.mysite.com/?contact=fred (note the slash). Does it matter if I create links that don't include that slash?

It works fine without the slash in the two browsers I've tried, but I don't want to cause any unnecessary hiccups.

Thanks in advance,
Prem.

jdMorgan

2:40 am on May 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your server will add the slash (internally) if it is missing, but this causes it to have to do extra work. If your site is a busy one (or if you plan for it to be busy eventually), I'd add the slash to your links just to make things more efficient. Also, somewhere out there might be a robot or a browser that won't understand your URLs without slashes...

Jim

premasagar

8:19 am on May 24, 2004 (gmt 0)

10+ Year Member



Thanks, Jim. Good, clear answer.

Prem.

jdMorgan

1:10 pm on May 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Forgot to mention...

You can re-code that to make it smaller/faster:


RewriteCond %{QUERY_STRING} ^contact=(.*)
RewriteRule ^$ /misc/contact.php?contact=%1 [L]

This does exactly the same thing as the code you posted, but avoids using ".*" as the RewriteRule pattern. It also moves the URI compare from an extra RewriteCond into the RewriteRule itself. As a result, it should improve your server performance a little on a busy site. Due to the way that mod_rewrite processes RewriteRules and RewriteConds, the RewriteCond in this case will now be evaluated only if the requested URI is blank or "/", and this saves time. See the Apache mod_rewrite documentation, "Ruleset Processing" for more info.

Jim

premasagar

1:56 pm on May 24, 2004 (gmt 0)

10+ Year Member



Thanks Jim. That's good to know.
Just one other thing...

Should this code work (it doesn't seem to)? It should match any request for www.mysite.com/contact where the query string is not "contact=..."

It should internally rewrite www.mysite.com/contact?fred to www.mysite.com/misc/contact.php?contact=fred


RewriteCond %{QUERY_STRING}!^contact=(.*) [NC]
RewriteRule ^contact$ /misc/contact.php?contact=%1 [L]

I am trying to capture the query string as a back-reference, but it doesn't seem to work when I am matching for a negative result (i.e. the "!" at the front of the RewriteCond).

Alternatively, I could put:


RewriteCond %{QUERY_STRING}!^contact=
RewriteCond %{QUERY_STRING} (.*) [NC]
RewriteRule ^contact$ /misc/contact.php?contact=%1 [L]

Prem.

jdMorgan

2:37 pm on May 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am trying to capture the query string as a back-reference, but it doesn't seem to work when I am matching for a negative result (i.e. the "!" at the front of the RewriteCond).

No, it won't work. You cannot use the contents of a negative match to create a back-reference. This is documented in the mod_rewrite documentation.

Alternatively, I could put:

RewriteCond %{QUERY_STRING} !^contact=
RewriteCond %{QUERY_STRING} (.*) [NC]
RewriteRule ^contact$ /misc/contact.php?contact=%1 [L]

That should work. There's no need for the [NC] flag on the second RewriteCond, though, since ".*" matches anything.

Jim