Reputation: 102
I am trying to get 3 mod_rewrites in a .htaccess file to work together.
I want the following:
school.mysite.com => mysite.com/index.php?inst=school
mysite.com/french => mysite.com/subject.php?sub=french
school.mysite.com/french => mysite.com/subject.php?inst=school&sub=french
I had previously got the first two working but could not for the life of me get the 3rd one to work. After a week I gave in and asked for help here, luckily for me anubhava gave me the solution really quickly with :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^([a-zA-Z_]+)\.mysite\.com$ [NC]
RewriteRule ^([a-zA-Z_]+)/?$ index.php?inst=%1&sub=$1 [L,QSA]
The code on its own works flawlessly but me wanting the moon on a stick also wants the other 2 to still work along side it... and they don't.
Since then I have been working on them non-stop and can get the subject one working along with it with the following code
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/([a-zA-Z_]+)/?$
RewriteRule ^(.*)$ subject.php?sub=%1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^([a-zA-Z_]+)\.mysite\.com$ [NC]
RewriteRule ^([a-zA-Z_]+)/?$ index.php?inst=%1&sub=$1 [L,QSA]
but I cannot get the sub-domain working along side it, my attempt is...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^([a-zA-Z_]+)\.mysite\.com$ [NC]
RewriteCond %{REQUEST_URI} ^$
RewriteRule ^(.*)$ index.php?institute=%1 [L]
I thought by checking if the URI was empty it might trigger it. Rather than bash my head against the wall for another week, I thought I would ask at day end instead :) If it makes any difference they are in the .htaccess file in the order of subdomain check, subject check and then the combined check.
I'm assuming my logic is flawed somewhere, ,please help.
regards
Zen
Upvotes: 0
Views: 82
Reputation: 22831
It should work if you order them as follows:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^([a-zA-Z_]+)\.mysite\.com$ [NC]
RewriteRule ^([a-zA-Z_]+)/?$ subject.php?inst=%1&sub=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^([a-zA-Z_]+)\.mysite\.com$ [NC]
RewriteRule ^/?$ index.php?inst=%1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^([a-zA-Z_]+)/$ subject.php?sub=$1 [L,QSA]
The first will match a subdomain AND a request uri, the second a subdomain with a blank uri, and the third for no subdomain with a uri. The order of the first two is important, the third one could be first if you wanted.
Upvotes: 1