homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

rewrite rule, 2 sites & 2 different results

 7:37 pm on Aug 9, 2008 (gmt 0)

Why does the exact same rewrite rule produce different results when used on different sites?

Both sites I tested are on the same server and httpd.conf settings are the same for each.

I'm just trying to redirect without showing that the url changed. One site will do it and the other shows the url that you're redirected to.



 1:15 am on Aug 10, 2008 (gmt 0)

Rather hard to tell without seeing the code...



 2:29 am on Aug 10, 2008 (gmt 0)

It's just basic stuff, nothing special.

Options +FollowSymLinks
RewriteEngine on
RewriteRule .* /country/us.php [L]
order allow,deny
allow from all

It redirect everyone from the US to this page.

One site shows the actual page url and the other site shows the url you entered before being redirected.


 2:51 am on Aug 10, 2008 (gmt 0)

I can't see any reason why the two sites would behave differently here.

However, there is a potential 'infinite loop' or 'dead-loop' problem. Consider changing the rule to:
RewriteRule !^country/us\.php$ /country/us.php [L]
to prevent the script URL-path from being repeatedly rewritten to itself.

And note that for purposes of accurate discussion, your rule is intended to do an internal URL-to-filename rewrite, not an external URL-to-URL redirect.



 3:59 am on Aug 10, 2008 (gmt 0)

Yeah I stop the infinate loop by turning the rewrite engine off in the /country folder. You got on to me about that one other time, but I think this way has 3 or 4 times less rewrite processes. When you multiply that by 100 million per day it could make a difference.

About the url-to-filename, I'll have to look up exactly what that means.


 4:30 am on Aug 10, 2008 (gmt 0)

The results of these rewrite rules could vary if there are other higher level .htaccess files present that are not identical, in the two site structures.


 4:33 am on Aug 10, 2008 (gmt 0)

Simple: The two main forms of RewriteRule are an internal rewrite and an external redirect.

# Rewrite requests for x.html to y.html
RewriteRule ^x\.html$ /y.html [L]
# Redirect request for x.html to y.html
RewriteRule ^x\.html$ http://www.example.com/y.html [R=301,L]

An internal rewrite 'points' a request for a URL to a different file than it might resolve to without the RewriteRule. An internal rewrite says, "If we get a request for URL 'x.html', serve the file 'y.html' instead."

An external redirect sends an HTTP response to the client (e.g. browser or robot) that tells it to issue another HTTP request and ask for the resource it wanted using a different URL. So a redirect rule says, "If we get a request for URL 'x.html', redirect the client to http://www.example.com/y.html". When a client browser requests this new URL, it changes the URL in its address bar.

So, your problem is that on one server, your RewriteRule which is intended to do an internal rewrite is resulting in an external redirect.

Without knowing what other rules you use in your various .htaccess files and in httpd.conf, conf.d, etc., it's hard to tell what might be going on. The first thing I would check is to be sure that UseCanonicalName is off, or that the ServerName is exactly the one that you use as your canonical name. Otherwise, you may get into trouble with a conflict between the defined canonical name of the server and any RewriteRules intended to force a different canonical name.


[edited by: jdMorgan at 4:35 am (utc) on Aug. 10, 2008]


 3:15 pm on Aug 10, 2008 (gmt 0)

Oh so you were talking about my incorrect use of the word redirect when I'm actually doing a rewrite, gotcha.

I just figured out what the problem was and it seems to work now.

On the /country/us.php page there is php code that looks for a certain string in the url. If that string is set then it does a header redirect based on the string info, if it's not set then it redirects to a default page for that country.

So my little piece of php was the whole problem. Sorry for wasting your time, it's another learning experience I guess.


 7:38 pm on Aug 10, 2008 (gmt 0)

If you learnt something then it wasn't a waste of time. :-)

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved