Forum Moderators: bakedjake

Message Too Old, No Replies

RewriteBase and RewriteRule

The rewrite just isn't happening

         

mipapage

10:51 pm on May 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello, yet another mod_rewrite problem... I have searched high and low, but not sure I get it yet...
I need to take an url of
mydomain.com/a1.html
and return the output from
mydomain.com/mytemplate.php?page=a1
Simple, no?
Well, add in the fact that I need to use RewriteBase, and no longer so simple.

I can simulate at home on my server and get results with:
RewriteEngine on
RewriteRule ^(.*)\.html$ /mytemplate.php?page=$1

[edited by: mipapage at 10:58 pm (utc) on May 21, 2003]

mipapage

10:55 pm on May 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Continued cause it wouldn't let me write more..

But once on the live site I am asked to stick a 'RewriteBase /' into the .htaccess file, at which point things stop working.

When I print my variables, instead of returning 'a1', it returns either www./ai or _./a1 ...

Any ideas as to what may be happening?

jdMorgan

1:00 am on May 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mipapage,

> on the live site I am asked to stick a 'RewriteBase /' into the .htaccess file

Who asks you to do this? What happens if you leave it out?

Sometimes, looking at the error log files will give a good indication of when you really must use RewriteBase, because the rewritten request will generate a 404-Not Found error in that case.

Ref: [httpd.apache.org...]

Jim

mipapage

9:22 am on May 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jdMorgan,

Thanks for the reply. At some point last night my host changed a few settings and now it's good as gold.

However, with the rewrite rule in place, I can access the files with both the clean and the 'dirty' url.
what are the tecnical names for those? Canonical and?

How can I make it so that only the clean url gets access?

Here's the rewrite that is working:
RewriteEngine on
RewriteRule ^(.*)\.html$ /template.php?page=$1 [L]
very simple stuff, I know


Sometimes, looking at the error log files will give a good indication of when you really must use RewriteBase, because the rewritten request will generate a 404-Not Found error in that case.

One of those intuitively obvious things that I always miss, and proves that I have no talent for seeing the intuitively obvious.

jdMorgan

1:54 pm on May 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mipapage,

I'm not sure exactly what the question is, but you could block spiders from indexing the script URLs by using robots.txt.

User-agent: *
Disallow: /template

I don't think it's necessary, though.

Since the redirect is internal, blocking by referrer, etc., would not work either. But no-one will ever "see" that /template.php?page=a1 script address - it is used only inside your server. Users and spiders will see only the mydomain.com/a1.html - type URLs.

As to my comment about the error log file, I really meant that sometimes the error log will show you an error on a screwed-up looking file path, and that helps to figure out what rewritebase is needed. Other times, the error log is useless. So, it wasn't intended as any kind of snide remark at all.

Jim

mipapage

2:26 pm on May 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure exactly what the question is, but you could block spiders from indexing the script URLs by using robots.txt.

What I thought was that by placing the rewrite rule, you would no longer be able to put the ...com/mytemplate.php?page=1 in the address bar and have it work.

Upon reflection, I can't see why that would be the case - it shoudl still work, no?

Is there some sort of check that I can put in that says: if it doesn't end with .html, it doesn't cut it.

I do have something in my PHP that checks the variables...

jdMorgan

3:14 pm on May 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mipapage,

You could try this, which will redirect external script requests to your index page. Order is critical.


RewriteEngine on
RewriteRule ^(.*)\.html$ /template.php?page=$1 [L]
RewriteRule \.php$ / [R=301,L]

If your index page is not available using "www.yourdomain.com/", then use

RewriteRule \.php$ /index.html [R=301,L]

instead of the second rule.

Trying to redirect "anything but .html" would block your scripts, css, and images.

HTH,
Jim

mipapage

7:30 pm on May 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jdMorgan,

Thanks again for the reply! Here's what happened...Oh, first, important info: if I type in only the domain, I get a blank white page, therefore this condition:

If your index page is not available using "www.yourdomain.com/"

is true. But I tried both methods anyway


Results: Both methods seemed to throw my browser into loops, never really downloading the page.

Not sure if those results tell you anything. I'm going to try a couple of other things here on the local server...

Thanks again...

mipapage

7:40 pm on May 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Update:

When I try the first method, it rewrites the URL in my add bar to www.mydomain.com/?page=a1. Same result if I put in the URL with all of the variables etc.

jdMorgan

8:22 pm on May 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm...

It sounds like your script (or something) is doing an external redirect downstream from the RewriteRules.

What is the name and filetype of your true index page? - Are all pages actually handled by the script?


RewriteEngine on
RewriteRule ^(.*)\.html$ /template.php?page=$1 [L]
RewriteRule \.php$ / [R=301,L]

In this code, the first Rule uses a transparent redirect, and then the rewriting engine quits because of the [L] flag. At this point, the template.php script is called and it should serve the requested content. If, however, it calls for a 301 or 302 external redirect - one that sends a 301 or 302 response back to the browser, then the browser will issue a new request, and .htaccess will be invoked again, thus leading to a loop.

You can check whether the script issues an external 301 or 302 redirect by using the WebmasterWorld Server Headers [webmasterworld.com] checker. Start with a valid "html" URL, and see if you get a 301 or 302 response back from the script, instead of a 200-OK response and the page you asked for.

So, the choice is to either live with people being able to directly access your script, or going through the PHP code to make sure that all files are <include>d rather than referenced by external redirection.

A partial solution might be to stop non-blank external referrers from accessing your script; It won't cover all cases, but it may help:


RewriteEngine on
RewriteRule ^(.*)\.html$ /template.php?page=$1 [L]
EewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^(www\.)?mydomain\.com [NC]
RewriteRule \.php$ - [F]

Jim

mipapage

8:28 pm on May 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Jim,

Thanks a lot. I'm going to take a bit to digest this, but I thought I'd thank you for your time and expertise!

Off to read that post again!

mipapage

10:28 am on May 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jim,

Hope you're still with me on this, it's been a few days, but I had some work to do on the site, now I'm back to trouble shooting the rewrite...



What is the name and filetype of your true index page? - Are all pages actually handled by the script?

The true index page is called temaplate.php?page=index.
Yes, all pages are handles by the script.

I tried the Server Headers checker on one of the rewritten urls, and it returned "HTTP/1.1 200 OK".

My main problem is now that when you enter just the domain name, a blank page pops up. Any insight into this?

mipapage

10:50 am on May 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It appears that I managed to solve the problem of entering via the root URL by using a little gem that littleman posted in another thread. So simple, the answer, and all the while I was trying more complicated things...
[webmasterworld.com...]

RewriteBase / 
RewriteRule ^$ index.html [L]