Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get apache RewriteRule working correctly for a subdomain?

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?

like image 435
mike Avatar asked Nov 06 '22 14:11

mike


1 Answers

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] 
like image 176
TCCV Avatar answered Nov 24 '22 14:11

TCCV