Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule wildcard generates error 500

         

exastris

2:01 pm on Aug 31, 2007 (gmt 0)

10+ Year Member



I maintain a corporate site that has multiple domains pointing to the same document root, and one htaccess file servicing the whole thing. The file has about 400 lines all working perfectly. Today I'm trying to set up all pages under one domain to be handled by one php script as follows:

RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC]
RewriteRule ^(.*)$ /bla.php [L]

When I leave out the (.*) the line works fine but obviously only handles the home page. Putting in the (.*) produces an Internal Server Error. Anyone know why?

I spent several hours googling for a solution, to no avail. According to some sites, this line should work as-is. Others say to leave out the brackets, and/or the ^ and $, but all of that doesn't solve the problem.

If I preceed or follow the (.*) with a string, there's no problem. I have other lines in my htaccess that use only wildcards in the pattern, such as

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

and that works perfectly fine. However, that's a *redirect*, and in this case I don't want a redirect. I just want all paths and files under this domain to be *transformed* to a single script, like I do with all the other RewriteRules in my htaccess that *do* work.

I couldn't find the answer to this problem already in the forum; if it is there, please let me know?

[edited by: jdMorgan at 2:10 pm (utc) on Aug. 31, 2007]
[edit reason] example.com [/edit]

jdMorgan

2:10 pm on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's nothing in your code to stop requests for "bla.php" from being rewritten to "bla.php" -- so you've created an infinite loop. Try:

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{THE_REQUEST} !^/blah\.php$
RewriteRule .* /bla\.php [L]

or even more concisely:

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule !^blah\.php$ /bla\.php [L]

Also, don't end-anchor the hostname, or if you do, use a pattern like "^www\.example\.com(:80)?$" or "^www\.example\.com(:[0-9]{1,5})?$" or "^www\.example\.com(:[0-9]+)?$" to allow for valid port numbers.

Jim

exastris

3:04 pm on Aug 31, 2007 (gmt 0)

10+ Year Member



Ah, of course, makes sense. *feels stupid*

Thanks Jim, works like a charm now.