Forum Moderators: phranque

Message Too Old, No Replies

rewrite mod RewriteRule inquiry

         

wiglaf

7:29 pm on Jan 9, 2008 (gmt 0)

10+ Year Member



I'm new when it comes to using rewrite_mod on apache, and I seem to be having a problem that should be easy to figure out, but I have no idea what's going on.

I'm in a directory mysite.com/ and I want to rewrite all requests for a sub-directory to a php script. So mysite.com/subdir gets rewritten to mysite.com/script.php?var=subdir

What I came up with is this (assume RewriteEngine on):

rewriteRule ^(.*) /script.php?var=$1 [L]

But that returns a 500 internal error. What's weird is if I try

rewriteRule ^test/(.*) /script.php?var=$1 [L]

and give the server a request for mysite.com/test/subdir it works. Why does it work for one and not the other? Thanks.

jdMorgan

8:12 pm on Jan 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to explicitly prevent this from rewriting /script.php to itself in an 'infinite loop':

RewritCond $1 !^script\.php$
RewriteRule (.*) /script.php?var=$1 [L]

This is the most likely cause of the problem. Check your server error log to be sure. It should contain a very clear description of the problem that causes the 500-Server Error.

Jim

wiglaf

8:45 pm on Jan 9, 2008 (gmt 0)

10+ Year Member



Your solution worked. I looked through the documentation for mod_rewrite but could not deduce this on my own. I'm learning a lot here, so for clarity I have one more question.

When the regular expression matches my string in (.*) it gets rewriten to script.php with the query string attached. So my question: by rewriting to script.php there's a whole new http request that goes through a whole new iteration through the .htaccess file? That would make a lot of sense. For some reason I didn't think the .htaccess file would be iterated through more than once.

jdMorgan

4:15 am on Jan 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, there's no new HTTP request for an internal rewrite. There *is* a new HTTP request if you use the external redirect form of RewriteRule, but that's not the case here.

mod_rewrite rules are iterated in .htaccess so that further rewrites can be applied -- particularly those having to do with access control. Note that the [L] flag stops processing only within one such iteration.

Jim

wiglaf

9:15 pm on Jan 15, 2008 (gmt 0)

10+ Year Member



Thanks. I'm running into a new problem (feature) with use of the R flag. I'm in a directory on my server at http://example.com/dir/

RewriteRule ^people$ people/ [R]
RewriteRule ^people/$ peeps.php [L]

This returns a 404 error when requesting http://example.com/dir/people, but rewriting the first line as

RewriteRule ^people$ /dir/people/ [R]

does the trick (peeps.php is displayed). Also removing the R flag correctly displays peeps.php, but without the added forward slash that I want. I would like to not have to specify the /dir/ directory for the first line, in case I move the contents to a different place on the server. Do I always have to specify the full path from the web root when using the R flag?

[edited by: jdMorgan at 10:20 pm (utc) on Jan. 15, 2008]
[edit reason] example.com [/edit]

jdMorgan

10:18 pm on Jan 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Do I always have to specify the full path from the web root when using the R flag?

Yes, in fact, it's often a good idea to provide the full canonical URL so as not to have to deal with UseCanonicalName problems.


RewriteRule ^foo/bars\.html$ http:/www.example.com/few/bears.html [R=301,L]

Jim

wiglaf

10:56 pm on Jan 15, 2008 (gmt 0)

10+ Year Member



Thank you, you're an enormous help. I have another question on a related but different subject, so I'll just ask here rather than start a new thread.

It seems there's some apache directive in the httpd.conf file (which I don't have access to) that is making apache automatically handle php files without their extension. For example, requesting http://example.com/people refers to http://example.com/people.php (even if I have a rewrite rule that matches ^people$)

This is a bit troublesome, because then I have to make sure all the request URIs that I'm trying to match in my rewrite rules don't have a directory that shares it's name with a php file (minus the extension) in that directory.

What directive could be causing this, and is there any way to override it with directives in my .htaccess file? If not I'll have deal with the inconvenience of naming my php files with less intuitive names (not a big deal but I'm pretty curious as well).

jdMorgan

11:51 pm on Jan 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look into mod_negotiation and the AcceptPathInfo (Apache 2.x) core directive. If either are enabled, you can get the effect you describe, along with risking duplicate-content "penalties." As discussed in the SEO-oriented forums, make sure that for any page on your site, there is one and only one URL that can be used to reach that page. Non-www vs. www- (and/or wild-card) subdomains, "/index.php" vs. "/", http vs. https, spurious query strings -- All must be handled and redirected to a single canonical page URL. (Thought you had a small problem, eh) ;)

Jim

wiglaf

7:27 am on Jan 16, 2008 (gmt 0)

10+ Year Member



I did a bunch of research on what directives are responsible for this. It looks like MultiViews is to blame here, the apache documentation describes my situation exactly:

[httpd.apache.org...]

The problem is in turning it off. MultiViewsMatch in mod_mime doesn't have an option for not matching anything, and specifying -MultiViews in Options just serves up a 500 error (and oddly also if i specify +MultiViews).

The only thing I can think to do now is getting rid of all the mime types that are specified with mod_mime's directives for .php files, but I'm not even sure this would work (or even if it's a good idea), plus I have no access to httpd.conf. How do I find out where my error logs are for apache? I probably need to take a look at those, as well as the actual httpd.conf file. Any ideas?

jdMorgan

3:10 pm on Jan 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



500-Server Error

You'll need to ask your host to set the AllowOverride for your account to all you to use the Options directive, or to disable MultiViews for your account. If this cannot be done, then I'm afraid you'll need to change hosts; There are many good hosts available with less restrictive policies.

Jim

[edited by: jdMorgan at 3:10 pm (utc) on Jan. 16, 2008]