views:

268

answers:

4

I just setup a subdomain with the following RewriteCond:

RewriteCond $1 !^search.php$  
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^/?([^/]+)$ search.php?q=$1 [L,NS] 

I'm using the same rewrite condition on my main domain and it works perfectly. However, when I set it up on the subdomain, it simply outputs "index.php" when going to http://sub.domain.com

Every page on the subdomain outputs the page name in the body instead of processing the code, except for the search page, which appears to be working correctly.

What can I do to correct this issue?

A: 

Several things come to mind here:

I have a few suggestions/comments/gotchas. Hopefully one of them is useful to you:

  1. Make sure search.php isn't just echoing out its $_GET parameters. While this sounds obvious in retrospect, it's one of the more overlooked solutions.
  2. RewriteRule works slightly differently when you specify it in a server configuration file than if you specify it in an .htaccess. Specifically, ^/ is wrong in a server config version as the entire URL is used (http://sub.domain.com/blah).
  3. Make sure no other rewrite rules are being processed for this subdomain first, either in the main httpd.conf / apache2.conf or .htaccess.
  4. Make sure RewriteEngine On appears in your configuration, as it is activated per-VirtualHost.
  5. The NS flag will ignore redirects done using a relative Redirect or relative RewriteRule.
R. Bemrose
`^/` Works perfect, the entire domain is **not** used. Only the path.
webdestroya
Also if the RewriteEngine is activated in the .htaccess file make sure that AllowOverride is set to a proper value.
Killer_X
A: 

It sounds like the pattern '^/?([^/]+)$' may not be matching at all.

I'd activate RewriteLog, crank RewriteLogLevel to level 3 or above, and see if your pattern is matching at all. If not, start with a simpler pattern, and then work your way to a more complex pattern.

Or, something else is matching the pattern, so the request never gets to 'RewriteRule ^/?([^/]+)$' at all. You will see this in the RewriteLog.

I believe I recently had a problem where '^/' didn't match in certain cases on a Virtual Host. But '/' worked. The folks in the #httpd on Freenode.org helped me. If I can find this in my notes, I'll post it here.

Stefan Lasiewski
+1  A: 

I haven't played with your exact regex with mod_rewrite, but if I was looking at writing that regex in another engine, I would have to escape the slash. Also, given that $ is used to indicate a back reference, would that need escaping too (would your $ symbols in the regex be necessary as there is likely to be more text in the URI and it is not matched at the end of a string)?

I would try

RewriteCond $1 !^search.php$  
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^/?([^\/]+)$ search.php?q=$1 [L,NS] 

One other thing. Normally $ at the end of a regex means "only match if this is the end of the string". So from that, if RewriteCond is matching on ^search.php$ but the URL is search.php?q=... then I would think that this wouldn't match because search.php is not the end of the string. So that would look like the following (assuming you don't need to change anything else from your original).

RewriteCond $1 !^search.php
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^/?([^/]+)$ search.php?q=$1 [L,NS] 
Tim
+1  A: 

In the main config the path always begins with / and you need an absolute path:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^search.php$                                                                                                                                                                             
RewriteCond %{REQUEST_FILENAME} !-f                                                                                                                                                                                       
RewriteRule ^/([^/]+)$ %{DOCUMENT_ROOT}/search.php?q=$1 [L]   

In an .htaccess you need a RewriteBase which is stripped from the url (no / in the Rule now) and the path is relative.

RewriteEngine On
RewriteBase /                                                                                                                                                                                                             
RewriteCond %{REQUEST_FILENAME} !^search.php$                                                                                                                                                                             
RewriteCond %{REQUEST_FILENAME} !-f                                                                                                                                                                                       
RewriteRule ^([^/]+)$ search.php?q=$1 [L]   
llama